序
我是年前搭建的微服務版,springBoot版本2.2.2.RELEASE,springCloud版本Hoxton.SR1。我的微服整體架構是eureka註冊中心 + config配置中心 + gateway網關 + 服務若干。
一、先看看我的微服整體分支
data爲vo、dto、util集合,每個服務都引入。
二、再上我gateway裏的解決跨域Filter代碼
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; import org.springframework.cloud.gateway.config.GlobalCorsProperties; import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator; import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties; import org.springframework.cloud.gateway.route.RouteDefinitionLocator; 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; /** * 解決跨域 * @author zwsky */ @Configuration @ConditionalOnBean(GlobalCorsProperties.class) public class GwCorsFilter { private static final String ALL = "*"; private static final Long MAX_AGE = 18000L; @Bean public CorsWebFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); // 允許cookies跨域 config.setAllowCredentials(true); // #允許向該服務器提交請求的URI,*表示全部允許,在SpringMVC中,如果設成*,會自動轉成當前請求頭中的Origin config.addAllowedOrigin(ALL); // #允許訪問的頭信息,*表示全部 config.addAllowedHeader(ALL); // 預檢請求的緩存時間(秒),即在這個時間段裏,對於相同的跨域請求不會再預檢了 config.setMaxAge(MAX_AGE); // 允許提交請求的方法類型,*表示全部允許 config.addAllowedMethod(ALL); //配置前端js允許訪問的自定義響應頭 //config.addExposedHeader("X-Token"); config.addExposedHeader(ALL); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/*", config); return new CorsWebFilter(source); } /** * *如果使用了註冊中心(如:Eureka),進行控制則需要增加如下配置 */ @Bean public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(ReactiveDiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { return new DiscoveryClientRouteDefinitionLocator(discoveryClient,properties); } }
PS:網上有的說路徑應該是/**,我寫成/**的時候gateway啓動會報一個異常,說配置應該是/*。
這個解決跨域的Filter應該是沒毛病的,期間參考官網與其他博友的技術分享,基本都是這樣寫的。也有說gateway設置了這個Filter,其他服務不能再使用extends WebMvcConfigurationSupport重寫addCorsMappings的方法解決跨域
@Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*") .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") .allowedHeaders("Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma", "x-requested-with", "X-Token"); }
我每個服務都檢查了@Configuration註解的配置類,都沒有這樣代碼片段了。
三、可能的原因
更改springBoot爲2.2.6.RELEASE + springCloud爲Hoxton.SR1後,唯一就是跨域Filter返回的CorsWebFilter的版本:
會變爲spring-web:5.2.5RELEASE,我想應該還是agteway使用的WebFlux有關,下次看看源碼。幾天跟江南一點雨大佬哥也反饋了下,希望大佬哥能看到吧。
四、不看源碼下的折騰
新版本網上的資源也少,之前只聽說說過Spring Boot 2.2.4 發佈!緊急修復Spring Cloud Hoxton.SR1,2.2.6的不兼容問題還沒有大佬爆出。我這裏簡單的折騰是springBoot2.2.2後各個版本 + Hoxton.SR1/Hoxton.SR3,以及使用gateway的配置文件,然後跨域問題依然,爆出的一個錯誤也很奇葩:orgin “null”,Provisional headers are shown(F12調整,看header)
等一段時間,看看有沒有大佬爆出吧,下面由時間我也看看spring-web:5.2.5RELEASE與spring-web:5.2.2RELEASE是不是有區別。