taobao_model
tanghc 5 years ago
parent fc7ddef575
commit 760adf0f6d
  1. 49
      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;

Loading…
Cancel
Save