From 760adf0f6dff0b6b3cd7f117faac23fb3413859a Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 26 May 2020 10:02:22 +0800 Subject: [PATCH] 3.1.3 --- .../manager/AbstractConfiguration.java | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java index 45c15f21..3a784093 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java @@ -3,10 +3,10 @@ package com.gitee.sop.gatewaycommon.manager; import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.BeanInitializer; -import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor; import com.gitee.sop.gatewaycommon.bean.SpringContext; import com.gitee.sop.gatewaycommon.gateway.loadbalancer.NacosServerIntrospector; import com.gitee.sop.gatewaycommon.interceptor.MonitorRouteInterceptor; +import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor; import com.gitee.sop.gatewaycommon.limit.LimitManager; import com.gitee.sop.gatewaycommon.loadbalancer.SopPropertiesFactory; import com.gitee.sop.gatewaycommon.message.ErrorFactory; @@ -40,6 +40,7 @@ import org.springframework.context.event.EventListener; import org.springframework.core.env.Environment; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.filter.CorsFilter; import javax.annotation.PostConstruct; @@ -184,11 +185,35 @@ public class AbstractConfiguration implements ApplicationContextAware, Applicati /** - * 跨域过滤器 + * 跨域过滤器,zuul */ @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty("zuul.servlet-path") public CorsFilter corsFilter() { - return createCorsFilter(); + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", createCorsConfiguration()); + return new CorsFilter(source); + } + + /** + * 跨域过滤器,gateway + */ + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty("sop.gateway-index-path") + public CorsWebFilter corsFilterReact() { + org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source = new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", createCorsConfiguration()); + return new CorsWebFilter(source); + } + + private CorsConfiguration createCorsConfiguration() { + CorsConfiguration corsConfiguration = new CorsConfiguration(); + corsConfiguration.addAllowedOrigin("*"); + corsConfiguration.addAllowedHeader("*"); + corsConfiguration.addAllowedMethod("*"); + return corsConfiguration; } /** @@ -211,24 +236,6 @@ public class AbstractConfiguration implements ApplicationContextAware, Applicati return new EurekaServerIntrospector(); } - protected CorsFilter createCorsFilter() { - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - this.registerCorsConfiguration(source); - return new CorsFilter(source); - } - - protected void registerCorsConfiguration(UrlBasedCorsConfigurationSource source) { - source.registerCorsConfiguration("/**", corsConfiguration()); - } - - protected CorsConfiguration corsConfiguration() { - CorsConfiguration corsConfiguration = new CorsConfiguration(); - corsConfiguration.addAllowedOrigin("*"); - corsConfiguration.addAllowedHeader("*"); - corsConfiguration.addAllowedMethod("*"); - return corsConfiguration; - } - @Override public void run(ApplicationArguments args) throws Exception { this.isStartupCompleted = true;