【发布时间】:2021-07-24 08:36:18
【问题描述】:
Spring Cloud Gateway 作为具有以下授权配置的 OAuth2ResourceServer:
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges ->
exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(OAuth2ResourceServerSpec::jwt)
return http.build();
}
我有一个全局过滤器,负责在每个经过身份验证的有效请求中执行一些功能,如下所示:
@Service
public class CustomGlobal implements GlobalFilter {
@Autowired
BearerTokenAuthentication authentication;
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// access request headers, and perform some logic
// extract details from the JWT token, and perform some logic
log.info(authentication.getTokenAttributes.get("sub"));
// ^ in the above line there's a NullPointerException, since instance
// BearerTokenAuthentication is not set, or not visible at a GlobalFilter class
return chain.filter(exchange);
}
}
我仍处于学习阶段。任何可能的线索将不胜感激。
【问题讨论】:
标签: spring spring-security spring-webflux spring-cloud-gateway