https://blog.csdn.net/weixin_40760196/article/details/110225600
一、java类配制
@Bean
public CorsFilter corsFilter() {
logger.debug ("CORS限制打开");
CorsConfiguration config = new Corsconfiguration();#仅在开发环境设置为*
config.addAllowedorigin( "*");
config.addAllowedHeader("*");
config.addAllowedMethod("*" );
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration ( "/**",config);
return new CorsFilter(configsource);
二、yml配制
前端页面通过不同域名或IP访问SpringCloud Gateway,例如前端人员在本地起HttpServer直连服务器的Gateway进行调试。此时,同样会遇到跨域。需要在Gateway的配置文件中增加:
spring:
cloud :
gateway:
globalcors:
cors-configurations:
#仅在开发环境设置为*
'[ /**] ':
allowedOrigins: "*"
allowedHeaders: "*"
allowedMethods: "*"
网友评论