【问题标题】: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);
    }
}

【问题讨论】:

    标签: java spring security spring-security jwt


    【解决方案1】:

    “500 internal”错误是服务器异常期间出现的消息。我认为您应该使用“try and catch”来生成这些错误并选择其返回状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2017-07-08
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多