【发布时间】:2014-11-24 16:39:40
【问题描述】:
我有 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();
}
}
由于我的用户不是角色NON_EXISTING 的成员,因此测试 URL 显然不可访问。不幸的是,她是。
如果我将安全 authorizeRequests() 部分移动到配置类表单 1。在添加安全过滤器旁边,它会按预期阻止访问。但在我的情况下,第二个配置似乎被忽略了。
我还调试了configure() 方法并注意到HttpSecurity 不是同一个对象,它有点味道。
任何提示我如何使这项工作非常受欢迎。
目标总结:
- 有一个
WebSecurityConfigurerAdapter,它添加了过滤器并且对库的用户隐藏 - 让用户定义自己的自定义端点安全
Spring boot 1.1.6-RELEASE
【问题讨论】:
-
在 XML 中拥有多个
WebSecurityConfigurerAdapter与拥有多个http元素相同。所以它们没有合并为一个,而是分开使用。 -
是的,这不是我现在所理解的。任何提示如何解决这个问题?将尝试扩展
BestSecurityConfig覆盖configure()并删除@Configuration...让我们看看:) -
... 它有效。我只是想知道它是否正确
-
@M. Deinum @ Jan Zyka 我有同样的问题。调试显示我的过滤器已注册。但它被忽略了。我想了解 xml 中的 http 元素是什么意思。我得到了 web.xml 参考,但是:/
标签: spring-security spring-boot