我对带有反应堆栈的 Spring Cloud Security 有这个奇怪的问题。我设置了 Spring Cloud Gateway,同时,我将其设置为 RsourceServer,因此在这种情况下,所有传入请求都将获得 Firebase 授予的 JWT。这是我的配置中的一些快照。
application.yml
设置 JWK 和 Issuer,告诉 spring security 如何验证 JWT。
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com
issuer-uri: https://securetoken.google.com/${project_name}
SecurityConfig
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
// @formatter:off
http
.csrf().disable()
.authorizeExchange()
.pathMatchers("/public").permitAll()
.anyExchange().authenticated().and()
.oauth2ResourceServer()
.jwt();
return http.build();
// @formatter:on
}
}
最后是控制器
TestController
仅出于测试目的,我创建了 Controller 来检查是否设置了安全上下文。
@RestController
@Slf4j
public class TestController {
@GetMapping("/public")
public String getPublic() {
if (SecurityContextHolder.getContext().getAuthentication() != null) {
throw new IllegalStateException("Public API should not have any AuthContext");
}
return "All shiny you got your PUBLIC data!";
}
@GetMapping("/private")
public String getPrivate(@AuthenticationPrincipal Jwt jwt) {
log.info(jwt.getTokenValue()); //in here jwt present as should be
if (SecurityContextHolder.getContext().getAuthentication() == null) {
throw new IllegalStateException("Oops.... Private API should have AuthContext");
}
return "All shiny you got your PRIVATE data!";
}
}
所以我非常沮丧,希望有人能在这里帮助我。根据所有 Spring Security 文档,在我看来,当 JWT 有效时,我应该保存我的安全上下文。
主要目的是能够使用ServletBearerExchangeFilterFunction
哪个尝试从中获取身份验证和令牌并将其传递给下游 WebClient 中的其他服务。但是,它是 NULL。
Bearer Token Propagation here 清楚地写着 SecurityContext 应该在那个时候已经有效。
任何建议将不胜感激。