title: AuthenticationSuccessHandler in Spring Security
body: <p>I've used spring security in a Spring Boot application and there are 2 types of users: one is an ADMIN, and one just a simple user. I get the data from a <code>DataSource</code>, then I execute an SQL query.</p>
  
  <p>My problem is with a <strong>redirection</strong>: for every user I have a different homepage. I'm trying to use to <code>AthenticationSuccessHandler</code>, but it won't work.</p>
  
  <p>Please help.</p>
  
  <hr>
  
  <p>My Spring security class configuration : </p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.builders.WebSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
  
  import javax.sql.DataSource;
  
  @Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      Securityhandler successHandler;
  
      // Pour l'authentification des Utilisateur de Table Utilisateur
      @Autowired  
      public void GlobalConfig(AuthenticationManagerBuilder auth,DataSource dataSource) throws Exception {
          auth.jdbcAuthentication()
              .dataSource(dataSource) 
              .usersByUsernameQuery("SELECT  \"Pseudo\" AS principal , \"Password\" AS  credentials , true FROM \"UTILISATEUR\" WHERE \"Pseudo\" =  ? ")
              .authoritiesByUsernameQuery("SELECT  u.\"Pseudo\" AS principal , r.role as role  FROM \"UTILISATEUR\" u ,\"Role\" r where u.id_role=r.id_role AND \"Pseudo\" = ?  ")
              .rolePrefix("_ROLE");
      }
  
      // ne pas appliqué la securité sur les ressources 
      @Override
      public void configure(WebSecurity web) throws Exception {
          web.ignoring()
          .antMatchers("/bootstrap/**","/css/**");
  
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf().disable()   
              .authorizeRequests()
              .anyRequest()   
                  .authenticated()        
                  .and()
                  .formLogin()
                  .loginPage("/login")
                  .permitAll()
                  .successHandler(successHandler);
      }
  
  }
  </code></pre>
  
  <p><br>
  And this is my <em>AuthenticationSuccessHandler</em>:</p>
  
  <pre><code>import java.io.IOException;
  import java.util.Set;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.authority.AuthorityUtils;
  import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  
  public class Securityhandler implements AuthenticationSuccessHandler {
  
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
          Set&lt;String&gt; roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
          if (roles.contains("ROLE_Admin")) {
              response.sendRedirect("/admin/home.html");
          }
      }
  }
  </code></pre>
  
  <p><br>
  And this is the error in the console:</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 
    'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration':
    Injection of autowired dependencies failed;</p>
  </blockquote>