title: How to pass JwtFilter exception to server JSON answer?
body: <p>My JwtFilter: </p>
  
  <pre><code> public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
  
      public JWTAuthorizationFilter(AuthenticationManager authManager) {
          super(authManager);
      }
  
      @Override
      protected void doFilterInternal(HttpServletRequest req,
                                      HttpServletResponse res,
                                      FilterChain chain) throws IOException, ServletException {
          String header = req.getHeader("Authorization");
  
          if (header == null || !header.startsWith("Bearer ")) {
              chain.doFilter(req, res);
              return;
          }
  
          UsernamePasswordAuthenticationToken authentication = getAuthentication(req);
  
          SecurityContextHolder.getContext().setAuthentication(authentication);
          chain.doFilter(req, res);
      }
  
      private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
          String token = request.getHeader("Authorization");
          if (token != null) {
              // parse the token.
  
  
              byte[] keyBytes = Decoders.BASE64.decode("kaslsafdhlkjsdfhlksjdafhlaksjdfhlaskajdhflaksdaf87687687asdafasdhfasdfsdfdfjalskjfhlaskdhflasdjhl3434");
              Key key = Keys.hmacShaKeyFor(keyBytes);
  
              String cleanToken = token.replace("Bearer ", "");
              String user = Jwts.parser().setSigningKey(key).parseClaimsJws(cleanToken).getBody().getSubject();
  
              if (user != null) {
                  return new UsernamePasswordAuthenticationToken(user, null, new ArrayList&lt;&gt;());
              }
              return null;
          }
          return null;
      }
  }
  </code></pre>
  
  <p>And it throws "JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted." exception.</p>
  
  <p>I try to catch it with <code>authenticationEntryPoint(new JwtAuthenticationEntryPoint())</code>:</p>
  
  <pre><code>    @Component("restAuthenticationEntryPoint")
  public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
  
      public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
  
          response.setContentType("application/json");
          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
          response.getOutputStream().println("{ \"error\": \"" + authenticationException.getMessage() + "\" }");
  
      }
  
  }
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>{
      "error": "Full authentication is required to access this resource"
  }
  </code></pre>
  
  <p>How to get exceptions from filters and pass to http answer? </p>