gateway跨域问题
常见的跨域解决方案
1.在控制层或控制层方法添加@CrossOrigin注解
2.在服务层写入
/**
- 解决异步访问跨域
/
//@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//本应用的所有方法都会去处理跨域请求
registry.addMapping("/**")
//允许远端访问的域名
.allowedOrigins(“http://localhost:8080”)
//允许请求的方法(“POST”, “GET”, “PUT”, “OPTIONS”, “DELETE”)
.allowedMethods("")
//允许请求头
.allowedHeaders("*");
}
}
3.在gateway配置中加入
spring:
cloud:
# CORS配置
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- HEAD
- POST
- PUT
- DELETE
- TRACE
- OPTIONS
- PATCH
allowedHeaders: "*"
4.在gateway加CorsConfig
<