如何解决每个异常都会发送内部服务器错误
我已经编写了一个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”错误是服务器异常期间发生的消息。我认为您应该使用“尝试并捕获”来生成这些错误并选择其返回状态。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。