Table of Contents
引言
什么是跨域(CORS)
什么情况会跨域
解决方案
前端解决方案
后端解决方案
具体方式
一、使用Filter方式进行设置
二、继承 HandlerInterceptorAdapter
三、实现 WebMvcConfigurer
四、使用Nginx配置
五、使用@CrossOrgin注解
Spring Cloud Gateway 跨域配置
引言
我们在开发过程中经常会遇到前后端分离而导致的跨域问题,导致无法获取返回结果。跨域就像分离前端和后端的一道鸿沟,君在这边,她在那边,两两不能往来.
什么是跨域(CORS)
跨域(CORS)是指不同域名之间相互访问。跨域,指的是浏览器不能执行其他网站的脚本,它是由浏览器的同源策略所造成的,是浏览器对于JavaScript所定义的安全限制策略。
什么情况会跨域
同一协议, 如http或https
同一IP地址, 如127.0.0.1
同一端口, 如8080
以上三个条件中有一个条件不同就会产生跨域问题。
解决方案
前端解决方案
使用JSONP方式实现跨域调用;
使用NodeJS服务器做为服务代理,前端发起请求到NodeJS服务器, NodeJS服务器代理转发请求到后端服务器;
后端解决方案
nginx反向代理解决跨域
服务端设置Response Header(响应头部)的Access-Control-Allow-Origin
在需要跨域访问的类和方法中设置允许跨域访问(如Spring中使用@CrossOrigin注解);
继承使用Spring Web的CorsFilter(适用于Spring MVC、Spring Boot)
实现WebMvcConfigurer接口(适用于Spring Boot)
具体方式
一、使用Filter方式进行设置
使用Filter过滤器来过滤服务请求,向请求端设置Response Header(响应头部)的Access-Control-Allow-Origin属性声明允许跨域访问。
@WebFilterpublicclassCorsFilterimplementsFilter{@OverridepublicvoiddoFilter(ServletRequest req, ServletResponse res, FilterChain chain)throwsIOException, ServletException{
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Methods","*");
response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers","*");
response.setHeader("Access-Control-Allow-Credentials","true");
chain.doFilter(req, res); } }
二、继承 HandlerInterceptorAdapter
@ComponentpublicclassCrossInterceptorextendsHandlerInterceptorAdapter{
@OverridepublicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throwsException{
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"); response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers","*");
response.setHeader("Access-Control-Allow-Credentials","true");returntrue; }}
三、实现 WebMvcConfigurer
@Configuration@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
publicclassAppConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
// 拦截所有的请求.allowedOrigins("http://www.abc.com")
// 可跨域的域名,可以为 *.allowCredentials(true)
.allowedMethods("*")
// 允许跨域的方法,可以单独配置.allowedHeaders("*");
// 允许跨域的请求头,可以单独配置}}复制代码
四、使用Nginx配置
location / { add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
if ($request_method = 'OPTIONS') { return 204; }}复制代码
五、使用@CrossOrgin注解
如果只是想部分接口跨域,且不想使用配置来管理的话,可以使用这种方式
在Controller使用
@CrossOrigin@RestController@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public User get(@PathVariable Long id) {
}@DeleteMapping(
"/{id}"
)
public void remove(@PathVariable Long id) {
}
}
在具体接口上使用
@RestController
@RequestMapping
("/user")public class UserController
{
@CrossOrigin@GetMapping
("/{id}")public User get
(@PathVariable Long id)
{
}
@DeleteMapping("/{id}")
public void remove(
@PathVariable Long id) {
}
}
Spring Cloud Gateway 跨域配置
spring☁️gateway:globalcors:cors-configurations:’[/**]’:# 允许跨域的源(网站域名/ip),设置为全部# 允许跨域请求里的head字段,设置为全部# 允许跨域的method, 默认为GET和OPTIONS,设置为全部allow-credentials:trueallowed-origins:-“http://xb.abc.com”-“http://sf.xx.com"allowed-headers:”"allowed-methods:-OPTIONS-GET-POST-DELETE-PUT-PATCHmax-age:3600复制代码
注意:通过gateway转发的其他项目,不要进行配置跨域配置