Spring Cloud Gateway + oauth2 跨域配置實現
版本說明
spring-cloud-starter-gateway : 2.2.5RELEASE
spring-cloud-starter-oauth2 : 2.2.4RELEASE
spring-security-oauth2 : 2.3.8RELEASE
nacos跨域配置
spring:
cloud:
gateway:
globalcors:
add-to-simple-url-handler-mapping: true
corsConfigurations:
'[/**]':
# 支援跨域訪問的來源
allowedOrigins:
- "http://localhost:8080"
# 切記 allowCredentials 配置 為true時,allowedOrigins不能為 *
allowCredentials: true
# 瀏覽器跨域嗅探間隔 單位秒
maxAge: 86400
# 支援的方法 * 代表所有
allowedMethods: "*"
allowedHeaders: "*"
exposedHeaders: "setToken"
網上搜了一堆,大多都是上面的這些,但是如果只進行這樣的配置的話,根本不會生效,我這邊的gateway是整合了oauth2進行鑑權的,最後在Spring Security官方文件上看到了這一段
https://docs.spring.io/spring-security/site/docs/current/reference/html5/#cors
琢磨一番加參考其他部落格之後。。。
gateway新增跨域配置類
CorsConfig.java
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.config.GlobalCorsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
@Configuration
@EnableConfigurationProperties(GlobalCorsProperties.class)
public class CorsConfig {
@Order(Ordered.HIGHEST_PRECEDENCE)
@RefreshScope
@Bean
public CorsWebFilter corsWebFilter(GlobalCorsProperties globalCorsProperties){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
globalCorsProperties.getCorsConfigurations().forEach((k,v) -> source.registerCorsConfiguration(k, v));
return new CorsWebFilter(source);
}
}
再次編譯啟動專案,測試一波後,跨域配置成功生效
相關文章
- Spring Cloud Gateway 實現 gRpc 代理SpringCloudGatewayRPC
- 最全面的改造Zuul閘道器為Spring Cloud Gateway(包含Zuul核心實現和Spring Cloud Gateway核心實現)ZuulSpringCloudGateway
- Spring Cloud Gateway限流實戰SpringCloudGateway
- Spring Cloud Gateway + Nacos(1)簡單配置SpringCloudGateway
- nginx配置CORS實現跨域NginxCORS跨域
- Spring Cloud Security:Oauth2實現單點登入SpringCloudOAuth
- Spring Cloud Gateway實戰之二:更多路由配置方式SpringCloudGateway路由
- Spring Cloud實戰 | 最終篇:Spring Cloud Gateway+Spring Security OAuth2整合統一認證授權平臺下實現登出使JWT失效方案SpringCloudGatewayOAuthJWT
- Spring Cloud Gateway 深入SpringCloudGateway
- Spring cloud 之GatewaySpringCloudGateway
- 基於Redis實現Spring Cloud Gateway的動態管理RedisSpringCloudGateway
- Spring Cloud Gateway 實現簡單自定義過濾器SpringCloudGateway過濾器
- Spring Cloud Gateway實戰之一:初探SpringCloudGateway
- CORS方式實現ajax跨域 — nginx配置CORS跨域Nginx
- Spring Boot 通過CORS實現跨域Spring BootCORS跨域
- Spring Cloud Gateway示例 | DevGlanSpringCloudGatewaydev
- spring cloud gateway 不生效SpringCloudGateway
- Spring Cloud Gateway 限流操作SpringCloudGateway
- Spring Cloud Gateway 入門SpringCloudGateway
- Spring Cloud Alibaba實戰(七) - Gateway搭配Nacos實現動態路由SpringCloudGateway路由
- Spring Cloud :: Security :: OAuth2 – ⓪ OAuth2模型詳述SpringCloudOAuth模型
- 快速突擊 Spring Cloud GatewaySpringCloudGateway
- Spring Cloud Gateway初體驗SpringCloudGateway
- Spring Cloud Gateway 入門案例SpringCloudGateway
- Spring Cloud Gateway入坑記SpringCloudGateway
- Spring Cloud Gateway使用簡介SpringCloudGateway
- spring cloud gateway之filter篇SpringCloudGatewayFilter
- spring cloud gateway 之限流篇SpringCloudGateway
- Spring Cloud Config 實現配置中心SpringCloud
- 微服務閘道器實戰——Spring Cloud Gateway微服務SpringCloudGateway
- Spring Cloud Gateway實戰之五:內建filterSpringCloudGatewayFilter
- Spring Cloud Gateway實戰之三:動態路由SpringCloudGateway路由
- spring cloud gateway之服務註冊與發現SpringCloudGateway
- Spring Cloud Gateway入門 - spring.ioSpringCloudGateway
- spring-cloud-gateway靜態路由SpringCloudGateway路由
- Spring Cloud Gateway WebFilter工廠 | BaeldungSpringCloudGatewayWebFilter
- Spring Cloud Gateway (一)入門篇SpringCloudGateway
- Spring Cloud Gateway---GlobalFilter(入門)SpringCloudGatewayFilter