title: How to override the response body of AuthenticationFailure using AbstractPreAuthenticatedProcessingFilter?
body: <p>I'm using a Custom Filter to make the request Authetincation. Configuring the <code>AutheticationFailureHandler</code>, I want put the message error content, but it is attached to a existent default content, my goal is put together on the same body or just a new one.</p>
  <p>Handler impl.:</p>
  <pre><code> protected void configure(HttpSecurity httpSecurity) throws Exception {
  APIKeyAuthFilter apiKeyAuthFilter = new APIKeyAuthFilter(HEADER_USER_AGENT, HEADER_APIKEY);
  
  apiKeyAuthFilter.setAuthenticationFailureHandler((request, response, exception) -&gt; {
                  if(exception.getMessage().equals(MESSAGE_USER_AGENT_NOT_FOUND)) {
                      Map&lt;String, Object&gt; data = new HashMap&lt;&gt;();
                      data.put(&quot;message&quot;,MESSAGE_USER_AGENT_NOT_FOUND);
                      response.getOutputStream()
                              .println(objectMapper.writeValueAsString(data));
                  }
              });
  }
  </code></pre>
  <p>Response body:</p>
  <pre><code>{
      &quot;message&quot;: &quot;User-Agent not found.&quot;
  }
  {
      &quot;timestamp&quot;: &quot;2020-07-16T16:26:59.438+0000&quot;,
      &quot;status&quot;: 403,
      &quot;error&quot;: &quot;Forbidden&quot;,
      &quot;message&quot;: &quot;Access Denied&quot;,
      &quot;path&quot;: &quot;/api&quot;
  }
  </code></pre>
  <p>Like shows, this is returning two JSON contents. How I set just one or append to existent?</p>
  <p>Whole code:</p>
  <pre><code>public class APIKeyAuthFilter extends AbstractPreAuthenticatedProcessingFilter {
  
      private String userAgentRequestHeader;
      private String apiKeyRequestHeader;
  
      public APIKeyAuthFilter(String userAgentRequestHeader, String apiKeyRequestHeader) {
          this.userAgentRequestHeader = userAgentRequestHeader;
          this.apiKeyRequestHeader = apiKeyRequestHeader;
      }
  
      @Override
      protected RequestAuthVo getPreAuthenticatedPrincipal(HttpServletRequest request) {
          String userAgent = request.getHeader(userAgentRequestHeader);
          String apiKey = request.getHeader(apiKeyRequestHeader);
          RequestAuthVo requestAuthVo = new RequestAuthVo(userAgent,apiKey);
          return requestAuthVo;
      }
  
      @Override
      protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {
          return &quot;N/A&quot;;
      }
  
  }
  </code></pre>
  <p>Configure:</p>
  <pre><code>        @Override
          @Order(2)
          protected void configure(HttpSecurity httpSecurity) throws Exception {
                  APIKeyAuthFilter apiKeyAuthFilter = new APIKeyAuthFilter(HEADER_USER_AGENT, HEADER_APIKEY);
  
              apiKeyAuthFilter.setAuthenticationManager(authentication -&gt; {
                  RequestAuthVo requestAuthVo = (RequestAuthVo) authentication.getPrincipal();
  
                  if (!mapAuths.containsKey(requestAuthVo.getUserAgent())) {
                      throw new UserAgentNotFoundException(MESSAGE_USER_AGENT_NOT_FOUND);
                  }
  
                  if(mapAuths.containsKey(requestAuthVo.getUserAgent()) &amp;&amp; mapAuths.get(requestAuthVo.getUserAgent()).equals(requestAuthVo.getApiKey())) {
                      authentication.setAuthenticated(true);
                      return authentication;
                  }
  
                  return authentication;
              });
  
              ObjectMapper objectMapper = new ObjectMapper();
  
   apiKeyAuthFilter.setAuthenticationFailureHandler((request, response, exception) -&gt; {
                      if(exception.getMessage().equals(MESSAGE_USER_AGENT_NOT_FOUND)) {
                          Map&lt;String, Object&gt; data = new HashMap&lt;&gt;();
                          data.put(&quot;message&quot;,MESSAGE_USER_AGENT_NOT_FOUND);
                          response.getOutputStream()
                                  .println(objectMapper.writeValueAsString(data));
                      }
                  });
  
              httpSecurity.antMatcher(&quot;/**&quot;).
                      csrf().disable().
                      sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
                      and()
                      .addFilter(apiKeyAuthFilter).authorizeRequests().anyRequest().authenticated();
          }
  
      }
  </code></pre>