我有 2 个类扩展 WebSecurityConfigurerAdapter
.并且不能让他们一起工作。
思路如下:
WebSecurityConfigurerAdapter
仅将自定义过滤器添加到安全链中。过滤器进行一些自定义身份验证并保存 Authentication
进入 SecurityContext
.这通常工作正常。配置如下(省略导入): @Order(1)
@Configuration
@EnableWebMvcSecurity
public class BestSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private BestPreAuthenticationFilter ssoAuthenticationFilter;
@Bean
protected FilterRegistrationBean getSSOAuthenticationFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(ssoAuthenticationFilter);
// Avoid include to the default chain
filterRegistrationBean.setEnabled(false);
return filterRegistrationBean;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(ssoAuthenticationFilter, SecurityContextPersistenceFilter.class);
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Autowired
private BestAuthenticationProvider authenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
}
}
@ComponentScan
包含的库类并对自定义身份验证进行排序。显然他们想提供定制的HttpSecurity
保护edpoints。尝试类似: @Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/testUrl").hasRole("NON_EXISTING")
.anyRequest().authenticated();
}
}
显然测试 URL 不应该是可访问的,因为我的用户不是角色
NON_EXISTING
的成员.不幸的是,她是。如果我移动安全
authorizeRequests()
配置类表单的一部分 1. 在添加安全过滤器之后,它会按预期阻止访问。但在我的情况下,第二个配置似乎被忽略了。我还调试了
configure()
方法并注意到 HttpSecurity
不是有一点气味的同一个物体。任何提示我如何使这项工作非常受欢迎。
目标总结:
WebSecurityConfigurerAdapter
添加过滤器并对库的用户隐藏Spring Boot 1.1.6-RELEASE
最佳答案
定义一个特殊的接口(interface)
public interface ServiceWebSecurityConfigurer {
void configure(HttpSecurity http) throws Exception;
}
然后只有一个 ConfigurerAdapter:
public class MyConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired(required = false)
ServiceWebSecurityConfigurer serviceSecConfig;
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(). // whatever
if (serviceSecConfig != null) serviceSecConfig.configure(http);
http.authorizeRequests(). // whatever
}
}
然后在需要时在其他地方实现 ServiceWebSecurityConfigurer 。也可以有多个实现,只需将它们 Autowiring 为列表并迭代并在主配置中使用它们。
关于spring-security - 在 Spring Boot 中使用多个 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114269/