首页 > 其他 > 详细

gateway 跨域问题解决方案

时间:2021-06-05 18:07:51      阅读:9      评论:0      收藏:0      [点我收藏+]

?1.写个配置类CorsConfig?(如果不行,可以不写此类,和springcloud的版本有关)

package?net.youqu.micro.service.config;

import?org.springframework.context.annotation.Bean;
import?org.springframework.context.annotation.Configuration;
import?org.springframework.web.cors.CorsConfiguration;
import?org.springframework.web.cors.reactive.CorsWebFilter;
import?org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import?org.springframework.web.util.pattern.PathPatternParser;

/**
?*?description:
?*?java项目www.fhadmin.org
?*/
@Configuration
public?class?CorsConfig?{
????@Bean
????public?CorsWebFilter?corsFilter()?{
????????CorsConfiguration?config?=?new?CorsConfiguration();
????????config.addAllowedMethod("*");
????????config.addAllowedOrigin("*");
????????config.addAllowedHeader("*");

????????UrlBasedCorsConfigurationSource?source?=?new?UrlBasedCorsConfigurationSource(new?PathPatternParser());
????????source.registerCorsConfiguration("/**",?config);

????????return?new?CorsWebFilter(source);
????}
}

2.?配置?application.properties 方式

#--------gateway配置--------
#跨域配置
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedHeaders=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=true

yaml 方式

spring:
??cloud:
????gateway:
??????globalcors:
????????corsConfigurations:
??????????‘[/**]‘:
????????????allowCredentials:?true
????????????allowedHeaders:?‘*‘
????????????allowedMethods:?‘*‘
????????????allowedOrigins:?‘*‘



gateway 跨域问题解决方案

原文:https://blog.51cto.com/u_14622073/2869883

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!