无法捕获 InvalidBearerTokenException 异常

来源:8-5 搭建授权服务器(三)授权服务器配置

abulaka

2022-01-04

当 token 过期或是一个非法 token 的时候,在
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider

private Jwt getJwt(BearerTokenAuthenticationToken bearer)
方法里抛出了
InvalidBearerTokenException 异常。
我尝试使用下面的方式想捕获 InvalidBearerTokenException 异常,但是始终捕获不到。想问下是哪里漏东西了吗?
我参考过 Catching exception thrown in AuthenticationProvider

public class SecurityExceptionHandlerSupport implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException exception) throws IOException, ServletException {

        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");

        PrintWriter printWriter = response.getWriter();
        printWriter.write("message");
        printWriter.flush();
    }
}
写回答

1回答

接灰的电子产品

2022-01-05

你为什么不在方法调用的时候catch呢

0
1
abulaka
能详细点说怎么catch吗 我用的是第三方认证授权服务 我看当抛出 InvalidBearerTokenException 后 就被 BearTokenAuthenticationEntryPoint 的 commence 方法处理掉了
2022-01-06
共1条回复

Spring Security+OAuth2 精讲,打造企业级认证与授权

一站式掌握主流安全框架与行业解决方案,从容应对各种安全难题。

1042 学习 · 316 问题

查看课程