Java 每个异常都会发送内部服务器错误
我编写了一个jwt过滤器,用于执行数据库操作和控制获取的令牌,但我扩展的OncePerRequestFilter在抛出异常时总是发送http状态500“内部服务器错误”。我怎样才能修好它Java 每个异常都会发送内部服务器错误,java,spring,security,spring-security,jwt,Java,Spring,Security,Spring Security,Jwt,我编写了一个jwt过滤器,用于执行数据库操作和控制获取的令牌,但我扩展的OncePerRequestFilter在抛出异常时总是发送http状态500“内部服务器错误”。我怎样才能修好它 @Component public class JwtTokenFilter extends OncePerRequestFilter { @Autowired private TokenManager tokenManager; private IgniteReposit
@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内部”错误是在服务器异常期间发生的消息。我认为您应该使用“try-and-catch”来生成这些错误并选择其返回状态