title: How to send response from zuul pre type filter?
body: <p>I am using Netflix zuul as api gateway. In a pre type Authentication filter, I am validating jwt tokens. If the token is expired or invalid, the request is not forwarded to underlying services. The filter should block the request and send a response to the client. The http response status code should be 403.
  But when i send a http request using an expired token, my authentication filter is not sending response to the client. The http response status code is 500. What is the issue and how to resolve it? How to set the response status code to 403 and send the response to the client? I am attaching the code snippet of my authentication filter class.</p>
  <pre><code>@Component
  </code></pre>
  <p>@RequiredArgsConstructor
  public class AuthenticationFilter extends ZuulFilter {</p>
  <pre><code>private static final Logger logger = LoggerFactory.getLogger(AuthenticationFilter.class);
  
  private final GatewayTokenService gatewayTokenService;
  private final LogHelperService logHelperService;
  private final JwtUtils jwtUtils;
  private final AESUtils aesUtils;
  private final AuthenticationErrorLogService authenticationErrorLogService;
  private final ErrorLogService errorLogService;
  private ResponseModelDTO responseModelDTO;
  
  
  @Override
  public String filterType() {
      return &quot;pre&quot;;
  }
  
  @Override
  public int filterOrder() {
      return 2;
  }
  
  @Override
  public boolean shouldFilter() {
      return true;
  }
  
  @SneakyThrows
  @Override
  public Object run(){
  
      RequestContext context = RequestContext.getCurrentContext();
      try{
          String authHeader = logHelperService.getHeaderFromRequestContext(HeaderName.AUTHORIZATION);
          if (authHeader != null) {
  
              String encrypted_jwt = jwtUtils.parseToken(authHeader, TokenType.BEARER);
  
              if(gatewayTokenService.isJwtTokenBlackListed(encrypted_jwt))
                  throw new BlacklistedTokenException(&quot;token is blacklisted!&quot;);
              else{
                  if(jwtUtils.isJwtTokenValid(aesUtils.decrypt(encrypted_jwt))) {
                      logger.info(&quot;Token is ok!&quot;);
                      return null;
                  }
              }
          }
          return null;
  
      }
  
      catch (TokenDecryptionException | BlacklistedTokenException | ExpiredJwtException | SignatureException | MalformedJwtException | UnsupportedJwtException | IllegalArgumentException e){
          authenticationErrorLogService.saveAuthenticationErrorLog(e);
  
          context.setSendZuulResponse(false); // blocks the request
          responseModelDTO = ResponseModelDTO.builder()
                  .status(Status.ERROR.getValue())
                  .message(&quot;Invalid Token&quot;)
                  .correlation_id(getZuulCorrelationId())
                  .data(null).build();
          context.setResponseBody(new ObjectMapper().writeValueAsString(responseModelDTO));
          context.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
          return null;
      }
      catch (Exception e){
          errorLogService.saveLogs(e, ExceptionType.Handled_Exception);
          context.setSendZuulResponse(false);
          responseModelDTO = ResponseModelDTO.builder()
                  .status(Status.ERROR.getValue())
                  .message(&quot;Invalid Token&quot;)
                  .correlation_id(getZuulCorrelationId())
                  .data(null).build();
          context.setResponseBody(new ObjectMapper().writeValueAsString(responseModelDTO));
          context.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
          return null;
      }
  }
  
  private String getZuulCorrelationId(){
      RequestContext context = RequestContext.getCurrentContext();
      return context.getZuulRequestHeaders().get(&quot;x-correlation-id&quot;);
  }
  </code></pre>
  <p>}</p>