我真的不会把它放在这里,但我真的很困惑,
我想实现以下目标。
我在跑步
- Java 14
-
Spring Cloud Gateway
版本:Hoxton.SR3
-
Spring Boot
版本:2.2.5.RELEASE
现在我想将安全性集成到我的网关和所有下游微服务中。最终,我决定使用 Firebase 作为身份提供商 (IDP)。我的 Angular 应用程序将从 Firebase 获取 JWT 令牌,并将其在每个请求中发送到云网关。因此,网关将开始仅充当资源服务器,仅此而已。
这是我如何尝试的。
设置和Spring Cloud Gateway
同时充当资源服务器。
这里解释得很好Spring安全文档 https://docs.spring.io/spring-security/site/docs/5.2.1.RELEASE/reference/htmlsingle/#webflux-oauth2-resource-server.
这是我的配置的样子
@EnableWebFluxSecurity
public class ResourceServerSecurityConfiguration {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
// @formatter:off
http
.authorizeExchange()
.anyExchange().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
return http.build();
// @formatter:on
}
}
和application.yml
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://www.googleapis.com/service_accounts/v1/jwk/[email protected] /cdn-cgi/l/email-protection
issuer-uri: https://securetoken.google.com/{$app.name}
正如您在此 YAML 中看到的,我提供了 jwk-set-uri 和颁发者来验证传入的令牌。
至此,所有工作都已被接受。所有请求的 Authentication 标头中都必须包含有效的 JWT。
Next,
我希望使用我的网关WebClient
并调用多个服务来聚合前端的数据。
这是我如何尝试配置我的客户端。
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder()
.filter(new ServletBearerExchangeFilterFunction());
}
正如你所看到的它使用ServletBearerExchangeFilterFunction
这就是我真正的问题所在。
我已经检查过,当 Spring 配置 oauth2ResourceServer 时它使用NoOpServerSecurityContextRepository
。据我所知,到目前为止,这正是一个用于注册每个请求上下文的存储库。另外,我知道使用 NoOp 是有意义的,因为我们想要无状态。然而我不明白如何制作ServletBearerExchangeFilterFunction
正常工作并将我的代币传递给下游。
我现在花了很多时间试图找出正确的方法。
发现这个:Spring Boot 2 OIDC (OAuth2) 客户端/资源服务器未在 WebClient 中传播访问令牌 https://stackoverflow.com/questions/59478787/spring-boot-2-oidc-oauth2-client-resource-server-not-propagating-the-access
Github: https://github.com/spring-projects/spring-security/issues/7771 https://github.com/spring-projects/spring-security/issues/7771
即使根据这一点,我尝试做的事情也应该是合法且可能的。不知道我哪里错了。