问题描述
我已经编写了一个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);
}
}
解决方法
“ 500 internal”错误是服务器异常期间发生的消息。我认为您应该使用“尝试并捕获”来生成这些错误并选择其返回状态。