1.@CrossOrigin注解
springmvc提供了@CrossOrigin注解在controller层加上注解@CrossOrigin注解进行前后端的跨域问题的解决,代码如下:
@CrossOrigin
@PostMapping("/findAll2")
public Map findAll2(){
Map map=new HashMap();
List<Studentinfo> studentinfos = studentinfoMapper.selectList(null);
map.put("code",0);
map.put("msg","");
map.put("count",0);
map.put("data",studentinfos);
return map;
}
2.nginx (engine x)反向代理
需要下载安装nginx软件,打开nginx目录下conf目录里面nginx.conf文件,配置文件内容,可以配置客户端跨域或服务端跨域,两种方式解决跨域问题,具体配置百度。
3.springboot设置cors跨域
CORS(Cross-origin resource sharing-跨源资源共享)允许网页从其他域向浏览器请求额外的资源,例如字体,CSS或来自CDN的静态图像。 CORS有助于将来自多个域的网页内容提供给通常具有相同安全策略的浏览器。
//这里实不实现接口没有影响
@Configuration
public class WebConfig implements WebMvcConfigurer {
//解决跨域
@Bean
public CorsFilter corsFilter() {
CorsConfiguration conf = new CorsConfiguration();
conf.addAllowedHeader("*");
conf.addAllowedMethod("*");
conf.addAllowedOrigin("*");
//允许cookie
conf.setAllowCredentials(true);
conf.setMaxAge(3600L);
conf.addExposedHeader("set-cookie");
conf.addExposedHeader("access-control-allow-headers");
conf.addExposedHeader("access-control-allow-methods");
conf.addExposedHeader("access-control-allow-origin");
conf.addExposedHeader("access-control-max-age");
conf.addExposedHeader("X-Frame-Options");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", conf); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
本文介绍了三种解决SpringBoot+Vue前后端分离中跨域问题的方法:使用@CrossOrigin注解、通过nginx反向代理以及在SpringBoot中设置CORS跨域配置。

被折叠的 条评论
为什么被折叠?



