2017-01-05 9 views
-1

私はSpringブートアプリケーションに2つの異なるログインフローを持っています。スプリングセキュリティの複数ログインフィルタ

  1. ノーマルフォーム・ログイン
  2. ステートレスログイン

私は次のことを試してみましたが、それは常に第二つのフィルタに来ます。ステートレス認証の場合は/api/**を、その他の場合は通常のセッションフォームのログインに制限するにはどうすればよいですか?

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

答えて

0

全体として、スプリングセキュリティは複数のHTTPブロックを使用してこれを処理します。

複数の設定をネストする必要があります。これは、Java設定でこれを処理する方法です。重要なのは、これらの設定では、バネが正しい順序でロードされるように、「Order」注釈が必要です。特定のサブパス(APIなど)の設定は、「/」を含む設定の前にロードする必要があります。

だからあなたの例では、(より多くの並べ替えがちょうど分割を実証し、それは単純に作ることができる)、これに似たものになるだろう:

@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=?"); 
     } 
    } 
} 
関連する問題