登录 url 8001 点击登录访问的 请求 url 8888 两个 url 端口不一样 产生了跨域问题
跨源资源共享(CORS)corss-origin requests
添加响应头,配置当次请求允许跨域
Access-Control-Allow-Origin:支持哪些来源的请求跨域
Access-Control-Allow-Methods:支持哪些方法跨域
Access-Control-Allow-Credentials:跨域请求默认不包含cookie,设置为true可以包含cookie
Access-Control-Expose-Headers:跨域请求暴露的字段CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma。如果想拿到其他字段,就必须在Access-Control-Expose-Headers里面指定。
Access-Control-Max-Age:表明该响应的有效时间为多少秒。在有效时间内,浏览器无
须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果
该首部字段的值超过了最大有效时间,将不会生效。
可以使用注解@CrossOrigin
配置类 GatewayCorsConfig
@Configuration
public class GatewayCorsConfig {
@Bean
public CorsWebFilter getCorsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedHeader("*"); // 允许所有请求头跨域
configuration.addAllowedMethod("*"); // 允许所有请求方法跨域
configuration.addAllowedOrigin("*"); // 允许所有请求来源跨域
configuration.setAllowCredentials(true); //允许携带cookie跨域,否则跨域请求会丢失cookie信息
source.registerCorsConfiguration("/**",configuration);
return new CorsWebFilter(source);
}
}
配置文件 配置跨域
# 方式二: 配置文件
spring:
# 跨域
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]': #所有的请求
allowedOrigins: "*" #跨域处理允许所有的域
allowedMethods: #支持的方法
- GET
- POST
- PUT
- DELETE