title: Custom Success Handler in spring boot
body: <p>I followed the tutorial <a href="https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/" rel="nofollow noreferrer">https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/</a> to integrate jwt to my application. I'm trying to add a custom successhandler to my application. Unfortunately, the successhandler is not triggering upon successful login request. I'm trying to return a user object instead of empty response with token in header. My handler is </p>
  
  <pre><code>@Component
  public class AuthSuccessHandler implements AuthenticationSuccessHandler {
      @Autowired
      private ApplicationUserRepository userRepository;
      @Autowired
      private ObjectMapper objectMapper;
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
              Authentication authentication) throws IOException, ServletException {
          UserDetails user = (UserDetails) authentication.getPrincipal();
          AuthInfo authInfo = new AuthInfo();
          authInfo.setUser(userRepository.findOneByUserName(user.getUsername()));
          authInfo.setToken(response.getHeader("Authentication"));
          response.getWriter().write(objectMapper.writeValueAsString(authInfo));
          response.setContentType("application/json");
          response.setStatus(200);
      }
  
  }
  </code></pre>
  
  <p>I have configured it in <code>WebSecurityConfigurer.configure()</code> as </p>
  
  <pre><code>protected void configure(HttpSecurity http) throws Exception {
      http.cors().and().csrf().disable().authorizeRequests()
              .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
              .antMatchers(HttpMethod.GET, "/public/**").permitAll()
              .anyRequest()
              .authenticated()
              .and()
              .formLogin()
              .successHandler(successHandler)
              .and()
              .addFilter(new JWTAuthenticationFilter(authenticationManager(), securityConfig))
              .addFilter(new JWTAuthorizationFilter(authenticationManager(), securityConfig))
              // this disables session creation on Spring Security
  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
      }
  </code></pre>
  
  <p>@pvpkiran's solution didn't work. On debugging, I find that authenticationfilter is running twice and authorization filter is not triggering. Given below is the list of filters available in the application logs</p>
  
  <pre><code>Security filter chain: [
    WebAsyncManagerIntegrationFilter
    SecurityContextPersistenceFilter
    HeaderWriterFilter
    CorsFilter
    LogoutFilter
    JWTAuthenticationFilter
    JWTAuthorizationFilter
    RequestCacheAwareFilter
    SecurityContextHolderAwareRequestFilter
    AnonymousAuthenticationFilter
    SessionManagementFilter
    ExceptionTranslationFilter
    FilterSecurityInterceptor
  ]
  </code></pre>