AuthenticationSuccessHandler in Spring Security

Ich verwendet habe, spring security in einer Spring-Boot-Anwendung, und es gibt 2 Arten von Benutzern: man ist ein ADMIN, dem man nur eine einfache Benutzeroberfläche. Ich bekomme die Daten aus einer DataSource, dann führe ich eine SQL-Abfrage.

Mein problem ist mit einem Umleitung: für jeden Benutzer, ich habe eine andere homepage. Ich bin versucht zu verwenden, um AthenticationSuccessHandler, aber es wird nicht funktionieren.

Bitte helfen.


Mein Spring security-Klassen-Konfiguration :

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);
    }

}


Und das ist mein AuthenticationSuccessHandler:

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<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_Admin")) {
            response.sendRedirect("/admin/home.html");
        }
    }
}


Und dies ist der Fehler in der Konsole:

org.springframework.Bohnen.factory.BeanCreationException: Fehler
erstellen bean mit dem Namen
'org.springframework.Sicherheit.config.Anmerkung.web.- Konfiguration.WebSecurityConfiguration':
Injektion von autowired Abhängigkeiten ist fehlgeschlagen;

  • Und was erwartet wird, eine Instanz zu schaffen, die SecurityHandler? Es gibt keine @Component noch wird es erklärt, wie eine @Bean. Also auf die Instanz, die es jemals geben wird.
  • könnte es mir erklären, bin wirklich ein Neuling in spring security
  • Das hat nichts zu tun mit Spring security, das ist grundlegend Frühjahr, Verwendung... müssen Sie sagen, der Frühling, die Objekte zu instanziieren, sonst kann es nicht autowire diese Instanzen.
  • wie ich nicht bekommen, es ist von @autowire in fornt der name der Klasse, die wollen, um es zu injizieren Methoden
InformationsquelleAutor Kamel Mili | 2016-03-29
Schreibe einen Kommentar