8

Spring Security Reference section 5.7によれば、複数のセキュリティアダプタを定義することが可能です。異なる認証プロバイダで複数のWebSecurityConfigurerAdapterを使用する(WebアプリケーションのAPIおよびLDAPの基本認証)

私は同じことをやろうとしますが、成功することはありません。サーバーの再起動後、最初のx回は基本認証で正常に動作しますが、数回後にログイン(フォーム)ページにリダイレクトされます。これはAPI呼び出しではなくWebアプリケーションでのみ発生します。

マイコード:

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 

    @Configuration 
    @Order(1) 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private Environment env; 

     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication(). 
       withUser("admin").password("pw_test").roles(API_ROLE); 
     } 

     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/services/**") 
       .authorizeRequests() 
       .anyRequest().hasRole(API_ROLE) 
       .and() 
       .httpBasic() 
       .and() 
       .csrf() 
       .disable(); 
     } 
    } 

    @Configuration 
    @Order(2) 
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private Environment env; 

     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()); 
      auth.eraseCredentials(false); 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      // LDAP FORM AUTHENTICATION 
      http.authorizeRequests() 
       .antMatchers("/login.html").permitAll() 
       .antMatchers("/css/**").permitAll() 
       .antMatchers("/js/**").permitAll() 
       .antMatchers("/images/**").permitAll() 
       .anyRequest().authenticated() 
      .and().formLogin() 
       .failureUrl("/login.html?error=1") 
       .loginPage("/login.html") 
       .loginProcessingUrl("/j_spring_security_check") 
       .defaultSuccessUrl("/success.html") 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .permitAll(); 

      http.csrf().disable(); 

      // iFRAMES SETTINGS 
      http 
       .headers() 
       .frameOptions().sameOrigin() 
       .httpStrictTransportSecurity().disable(); 

      // HTTPS 
      http 
       .requiresChannel() 
       .anyRequest() 
       .requiresSecure(); 

      //MAP 8080 to HTTPS PORT 
      http.portMapper().http(8080).mapsTo(443); 
     } 

     @Bean 
     public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
      CustomLdapAuthenticationProvider provider = new CustomLdapAuthenticationProvider(env.getProperty("ldap.domain"), env.getProperty("ldap.url"), env.getProperty("ldap.base")); 
      provider.setConvertSubErrorCodesToExceptions(true); 
      provider.setUseAuthenticationRequestCredentials(true); 
      return provider; 
     } 
    } 
} 

任意のアイデア?

私は、Springブートバージョン1.4.1-RELEASEとSpring Securityバージョン4.1.3-RELEASEを使用しています。

+0

以下の回答がありましたか? – theLearner

+0

はい、うまくいきます! – Dimi

答えて

2

同じAuthenticationManagerBuilderをautowireするので、両方の設定に同じAuthenticationManagerを使用します。

Spring Security Architectureを参照してください:

@Configuration 
public class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

    ... // web stuff here 

    @Autowired 
    public initialize(AuthenticationManagerBuilder builder, DataSource dataSource) { 
     auth.jdbcAuthentication().dataSource(dataSource).withUser("dave") 
      .password("secret").roles("USER"); 
    } 

} 

この例では、Webアプリケーションに関連するが、AuthenticationManagerBuilderの使用方法は、(Webアプリケーションセキュリティの実装方法の詳細については下記を参照)より広く適用可能です。 AuthenticationManagerBuilder@Beanのメソッドに@Autowiredであることに注意してください。それがグローバル(親)AuthenticationManagerを構築するものです。これとは対照的に、我々はこのようにそれをやっていれば:

@Configuration 
public class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 

    ... // web stuff here 

    @Override 
    public configure(AuthenticationManagerBuilder builder) { 
     auth.jdbcAuthentication().dataSource(dataSource).withUser("dave") 
      .password("secret").roles("USER"); 
    } 

} 

(コンフィギュラ方法の@Overrideを使用して)、その後AuthenticationManagerBuilderは唯一のグローバル1の子である、AuthenticationManager「ローカル」を構築するために使用されます。

関連する問題