2017-01-05 19 views
-1

Ich habe zwei verschiedene Login-Flows in meiner Spring Boot App.Spring Security mehrere Login-Filter

  1. Normalform Login
  2. Stateless Login

habe ich versucht, die folgenden, aber es kommt immer auf den 2. ein Filter. Wie schränke ich /api/**, um für statuslose Authentifizierung und andere auf normale Sitzung Formular Login zu beschränken?

@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    DataSource dataSource; 

    @Autowired 
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .jdbcAuthentication() 
       .dataSource(dataSource) 
       .usersByUsernameQuery("select username,password, enabled from users where username=?") 
       .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
    } 

    private final UserService userService; 

    private final TokenAuthenticationService tokenAuthenticationService; 

    public WebSecurityConfig() { 
     super(true); 
     this.userService = new UserService(); 
     tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .csrf().disable(); 

     // Normal form login using jdbc 
     http 
      .authorizeRequests() 
       .antMatchers("/", "/home").access("hasRole('ROLE_ADMIN')") 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .permitAll() 
       .and() 
      .logout().permitAll(); 
     //Stateless authentication using tokens - Custome authentication from token implemented in the filter 
     http 
      .authorizeRequests() 
       .antMatchers("/api").authenticated() 
       .and() 
      .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), 
      UsernamePasswordAuthenticationFilter.class); 
    } 
} 

Antwort

0

Insgesamt behandelt Federsicherheit dies durch Verwendung mehrerer HTTP-Blöcke.

Sie müssen mehrere Konfigurationen verschachteln - so behandelt die Java-Konfiguration dies. Wichtig ist, dass diese Konfigurationen die Annotation "Order" benötigen, um sicherzustellen, dass Spring sie in der richtigen Reihenfolge lädt - Konfigurationen für bestimmte Unterpfade (wie API) müssen vor der Konfiguration geladen werden, die "/" enthält.

So würde Ihr Beispiel wird etwas ähnlich wie diese (mehr Neuanordnung könnte es einfacher machen, nur die Spaltung demonstrieren):

@Configuration 
public class WebSecurityConfig { 

    @Autowired 
    private DataSource dataSource; 

    private final UserService userService; 

    private final TokenAuthenticationService tokenAuthenticationService; 

    public WebSecurityConfig() { 
     super(true); 
     this.userService = new UserService(); 
     tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService); 
    } 

    @Order(0) 
    @Configuration 
    private final class ApiSecurity extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.csrf().disable(); 

      //Stateless authentication using tokens - Custome authentication from token implemented in the filter 
      http.antMatchers("/api").authorizeRequests() 
       .authenticated().and() 
       .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), 
      UsernamePasswordAuthenticationFilter.class); 
     } 

     @Autowired 
     public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
      auth.jdbcAuthentication().dataSource(dataSource) 
      .usersByUsernameQuery("select username,password, enabled from users where username=?") 
      .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
    } 
    } 

    @Order(Ordered.LOWEST_PRECEDENCE) 
    @Configuration 
    private final class OtherSecurity extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.csrf().disable(); 

      //Normal form login using jdbc 
      http.antMatchers("/", "/home").authorizeRequests().access("hasRole('ROLE_ADMIN')").and().formLogin() 
       .loginPage("/login") .usernameParameter("username") 
       .passwordParameter("password").permitAll().and().logout().permitAll(); 
     } 

     @Autowired 
     public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
      auth.jdbcAuthentication().dataSource(dataSource) 
      .usersByUsernameQuery("select username,password, enabled from users where username=?") 
      .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
     } 
    } 
}