spring-boot - Spring Webflux -Security:当jwt令牌过期或错误时如何让Spring返回401(未授权)异常
问题描述
下面是授权 JWT 令牌(Keyclock)的代码,但如果出现异常,服务器永远不会返回 401
@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 验证?
解决方案
推荐阅读
- torch - 使用 cuda 10.1 安装 torch_geometric 及其依赖项
- javascript - 在 React 中给定时间后更新时间戳文本
- python - 如何在并行进程(python-asyncio)中将项目附加到列表中?
- php - 我们计算的请求签名与您提供的签名不匹配。在 php 中检查您的 AWS 秘密访问密钥和签名方法
- php - 发布新产品时如何获取 woocommerce 产品的所有详细信息?
- r - 有没有办法从 shinyapp.io 链接找到 R 代码?
- c++ - xcode 13 无法编译某些函数 c++
- python - 使用类中的函数关闭弹出窗口(kivy)
- sql - 有没有办法在忽略 Spring Boot 中的外键检查的同时截断表?
- anylogic - 步行图书馆的资源可用性