8

Theo số Spring Security Reference section 5.7, bạn có thể xác định nhiều bộ điều hợp bảo mật.Sử dụng nhiều WebSecurityConfigurerAdapter với AuthenticationProviders khác nhau (xác thực cơ bản cho API và LDAP cho ứng dụng web)

Tôi cố gắng làm tương tự nhưng không thành công. Sau khi khởi động lại máy chủ, lần đầu tiên x API hoạt động tốt với auth cơ bản, nhưng sau một vài lần tôi được chuyển hướng tới trang đăng nhập (biểu mẫu), điều này chỉ xảy ra cho ứng dụng web của chúng tôi, chứ không phải cho các cuộc gọi API.

Mã của tôi:

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

Bất kỳ ý tưởng?

Tôi đang sử dụng phiên bản Spring Boot 1.4.1-RELEASE và Spring Security phiên bản 4.1.3-RELEASE.

+0

làm việc dưới đây? – theLearner

+0

có, nó hoạt động tốt! – Dimi

Trả lời

2

Bạn sử dụng cùng một AuthenticationManager cho cả hai cấu hình, vì bạn tự động giống như AuthenticationManagerBuilder.

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

} 

dụ này liên quan đến một ứng dụng web, nhưng việc sử dụng AuthenticationManagerBuilder là rộng rãi hơn được áp dụng (xem dưới đây để biết thêm chi tiết về cách bảo mật ứng dụng web được thực hiện). Lưu ý rằng AuthenticationManagerBuilder@Autowired thành một phương thức trong một @Bean - đó là những gì làm cho nó xây dựng Trình quản lý xác thực toàn cầu (cấp độ gốc). Ngược lại, nếu chúng ta đã thực hiện nó theo cách này:

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

} 

(sử dụng một @Override của một phương pháp trong configurer) thì AuthenticationManagerBuilder chỉ được sử dụng để xây dựng một "địa phương" AuthenticationManager, mà là một con của một toàn cầu .

Các vấn đề liên quan