解决springcloud服务中gateway的跨域问题
步骤一:
编写类文件
@Configuration
public class CorsWebFiler {
@Bean
public CorsWebFilter getCorsFilter(){
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 允许cookies跨域
config.setMaxAge(1800L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);//拦截所有请求
return new CorsWebFilter(source);
}
}
步骤二:
配置配置文件
spring:
application:
name: gateway-web
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]': # 匹配所有请求
allowedOrigins: "*" #跨域处理 允许所有的域
allowedMethods: # 支持的方法
- GET
- POST
- PUT
- DELETE
- OPTIONS
## 路由配置
routes:
#管理
- id: ids_service_management #路由唯一标识
uri: lb://management # 发送给注册服务名字为xxx的服务器
predicates:
- Path=/equipmentAttributes/**,/setUpSystem/**,/MiningAreaToMapInfo/**,/shift/**,/shiftToEquipment/**,/MiningArea/**,/mapInfo/**,/malfunction/**,/userToEquipment/**,/user/**,/authority/**,/dict/**,/equipment/**,/line/**,/lineToStation/**,/role/**,/station/**,/findUserToRoleList/**,/authorityToRole/**,/menu/**,/menuToRole/**
filters:
- name: RequestRateLimiter
args:
key-resolver: "#{@ipKeyResolver}"
redis-rate-limiter.replenishRate: 100
redis-rate-limiter.burstCapacity: 200