- 引言
- 什么是跨域(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属性声明允许跨域访问。
public class CorsFilter implements Filter { | |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, 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(拦截器方式)
public class CrossInterceptor extends HandlerInterceptorAdapter { | |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |
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"); | |
return true; | |
} | |
} |
三、实现 WebMvcConfigurer
@Configuration | |
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") | |
public class AppConfig implements WebMvcConfigurer { | |
@Override | |
public void addCorsMappings(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: | |
cloud: | |
gateway: | |
globalcors: | |
cors-configurations: | |
'[/**]': | |
# 允许跨域的源(网站域名/ip),设置*为全部 | |
# 允许跨域请求里的head字段,设置*为全部 | |
# 允许跨域的method, 默认为GET和OPTIONS,设置*为全部 | |
allow-credentials: true | |
allowed-origins: | |
- "http://xb.abc.com" | |
- "http://sf.xx.com" | |
allowed-headers: "*" | |
allowed-methods: | |
- OPTIONS | |
- GET | |
- POST | |
- DELETE | |
- PUT | |
- PATCH | |
max-age: 3600 |
注意: 通过gateway
转发的其他项目,不要进行配置跨域配置
参考:https://cloud.tencent.com/developer/article/1668879