Dim*_*imi 13 java spring spring-security basic-authentication spring-boot
根据Spring Security Reference 5.7节,应该可以定义多个安全适配器.
我尝试做同样但没有成功.在服务器重新启动之后,API的前x次使用基本身份验证工作正常,但经过几次我被重定向到登录(表单)页面,这应该只针对我们的Web应用程序,而不是API调用.
我的代码:
@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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我正在使用Spring Boot版本1.4.1-RELEASE和Spring Security版本4.1.3-RELEASE.
您AuthenticationManager对两种配置都使用相同的配置,因为您自动装配相同的配置AuthenticationManagerBuilder.
请参阅Spring安全架构:
Run Code Online (Sandbox Code Playgroud)@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是@Autowired一个方法@Bean- 这使得它构建全局(父)AuthenticationManager.相反,如果我们这样做:Run Code Online (Sandbox Code Playgroud)@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仅用于构建"本地"AuthenticationManager,它是全局的子节点.
| 归档时间: |
|
| 查看次数: |
11469 次 |
| 最近记录: |