【发布时间】:2020-12-02 02:01:56
【问题描述】:
我已经编写了一个 jwt 过滤器来执行数据库操作和控制获取的令牌,但是我扩展的 OncePerRequestFilter 在抛出异常时总是发送 http 状态 500“内部服务器错误”。我该如何解决?
@Component
public class JwtTokenFilter extends OncePerRequestFilter {
@Autowired
private TokenManager tokenManager;
private IgniteRepository repository;
public JwtTokenFilter(IgniteRepository repository) {
this.repository = repository;
}
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest,
@NotNull HttpServletResponse httpServletResponse,
@NotNull FilterChain filterChain) throws ServletException, IOException {
//System.out.println(tokenManager.generateToken("qq"));
String authorizationHeader = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION);
if (Strings.isNullOrEmpty(authorizationHeader) || !authorizationHeader.startsWith("Bearer")) {
throw new JwtNullException();//this is my own exception class and it sends 401
}
String token = authorizationHeader.substring(7);
String afId = tokenManager.getUsernameToken(token);
repository.putCache(afId,new APIInvokerEnrolmentDetails());
//System.out.println(afId+" "+token);
if (!repository.containsAtCache(afId)){
throw new OnboardedAFException(); //this is my own exception class and it sends 403
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
【问题讨论】:
标签: java spring security spring-security jwt