每个异常都会发送内部服务器错误
Posted
技术标签:
【中文标题】每个异常都会发送内部服务器错误【英文标题】:each exception sends internal server error 【发布时间】: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);
【问题讨论】:
【参考方案1】:“500 internal”错误是服务器异常期间出现的消息。我认为您应该使用“try and catch”来生成这些错误并选择其返回状态。
【讨论】:
以上是关于每个异常都会发送内部服务器错误的主要内容,如果未能解决你的问题,请参考以下文章
处理 Django Rest Framework 中的 500 个内部错误