Spring boot SpringWebFlux-安全性:当jwt令牌过期或错误时,如何让Spring返回401(未经授权)异常
下面是授权JWT令牌(Keyclock)的代码,但在异常情况下,服务器永远不会返回401Spring boot SpringWebFlux-安全性:当jwt令牌过期或错误时,如何让Spring返回401(未经授权)异常,spring-boot,spring-security,keycloak,spring-security-oauth2,spring-webflux,Spring Boot,Spring Security,Keycloak,Spring Security Oauth2,Spring Webflux,下面是授权JWT令牌(Keyclock)的代码,但在异常情况下,服务器永远不会返回401 @EnableWebFluxSecurity public class SecurityConfig { @Bean public SecurityWebFilterChain securityWebFilterChain(final ServerHttpSecurity http) { // the matcher for all paths that need to be se
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securityWebFilterChain(final ServerHttpSecurity http) {
// the matcher for all paths that need to be secured (require a logged-in user)
http.authorizeExchange(exchanges -> exchanges.pathMatchers("/actuator/**").permitAll()
.pathMatchers("/abcde/auth").permitAll()
.pathMatchers("/abcde/auth/refresh").permitAll()
.anyExchange().authenticated())
.csrf().disable()
.oauth2ResourceServer(oauth2ResourceServer ->
oauth2ResourceServer
.jwt(withDefaults())
).exceptionHandling(exception-> exception.authenticationEntryPoint((swe, e) -> Mono.fromRunnable(() ->
{
swe.getResponse()
.setStatusCode(HttpStatus.UNAUTHORIZED);
}
)
)
);
return http.build();
}
另一个问题:
这段代码将只验证JWT令牌的到期,还是也验证其他验证?到底发生了什么我很想知道
简而言之,这段代码是否足以通过发卡机构URL进行keyclock JWT验证?如果您想确切知道,我建议您查看源代码。@ThomasAndolf我想您已经回答了我的第二个问题。第一个问题是,为什么401并没有通过http请求使用服务器日志401中提到的代码重新调整?您既没有编写它返回的内容,也没有发布您的日志logs@ThomasAndolf下面是处于调试模式的spring日志,但curl命令中没有响应。我确实希望是401。2020-01-06 15:49:43.085调试85386---[ctor-http-nio-4]o.s.w.s.adapter.HttpWebHandlerAdapter:[0c229084]已完成401未经授权的日志不要在这里发布日志,用日志更新您的问题,您的curl命令etcif您想知道的话,我建议您查看源代码。@Thomasand我想您已经回答了我的第二个问题。第一个问题是,为什么401并没有通过http请求使用服务器日志401中提到的代码重新调整?您既没有编写它返回的内容,也没有发布您的日志logs@ThomasAndolf下面是处于调试模式的spring日志,但curl命令中没有响应。我确实希望是401。2020-01-06 15:49:43.085调试85386---[ctor-http-nio-4]o.s.w.s.adapter.HttpWebHandlerAdapter:[0c229084]已完成401未经授权发布日志,使用日志更新问题,使用curl命令等