SpringBoot + VUE 项目中解决跨域问题三种方法

一、 启动前后端

二、前端访问后端接口

2.1 前端相关代码

<template>
  <div class="hello">
    <button @click="testCross">点我请求</button>
  </div>
</template>

import axios from "axios";


methods:{
    testCross(){
      axios.get("http://localhost:9090/testCross").then((res)=>{
        console.log(res)
      })
    }
  }

 2.2 后端相关代码

@RestController
@RequestMapping("/testCross")
public class TestCross {

    @GetMapping
    public List<String> testCross(){
        ArrayList<String> list = new ArrayList<>();
        list.add("AAA");
        list.add("BBB");
        list.add("CCC");
        return list;
    }
}

三、 前端访问后端接口报错

 四、解决方案

4.1 方案一(在目标方法上添加注解:@CrossOrigin)重启项目

 @GetMapping
    @CrossOrigin
    public List<String> testCross(){
        ArrayList<String> list = new ArrayList<>();
        list.add("AAA");
        list.add("BBB");
        list.add("CCC");
        return list;
    }

 4.2 方案二(注意:千万不要导错包)


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
 * @ClassName CrossConfig
 * @Description Cross过滤器
 * @Date 2022/3/25 11:22
 * @Version 1.0
 **/
@Configuration
public class CrossConfig {
    @Bean
    public CorsFilter corsFilter(){
        //1. 创建CorsConfiguration对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//设置允许那些域来访问,*是通配符,允许所有域
        corsConfiguration.addAllowedHeader("*");//请求头字段
        corsConfiguration.addAllowedMethod("*");//请求方式(GET,POST,DELETE,PUT)
        //设置source
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);//1.映射路径 2.传入CorsConfiguration对象
        return new CorsFilter(source);
    }
}

4.3 方案三(实现WebMvcConfigurer接口,重写它的addCorsMappings方法)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName CrossConfiguration
 * @Date 2022/3/25 11:36
 * @Version 1.0
 **/
@Configuration
public class CrossConfiguration implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//配置映射
                .allowedOriginPatterns("*")//设置允许那些域来访问
                .allowedHeaders("*")//请求头字段
                .allowedMethods("GET","POST","PUT","DELETE","HEAD","OPTIONS")//请求方式(GET,POST,DELETE,PUT)
                .allowCredentials(true)//是否允许携带cookie
                .maxAge(3600);//设置3600秒,表示3600秒之内浏览器不必再次询问

    }
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CC_Waiting

我,大学未工作,感谢您的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值