Spring 允许客户端应用程序从已连接的WebSecurity配置适配器配置http安全性
我使用的是spring boot和spring security 我为许多不同的项目设置了一个通用的Spring 允许客户端应用程序从已连接的WebSecurity配置适配器配置http安全性,spring,spring-boot,Spring,Spring Boot,我使用的是spring boot和spring security 我为许多不同的项目设置了一个通用的websecurityConfigureAdapter。问题是,我希望每个项目都有自定义的控制器安全性,其他一切都保持不变。最明显的解决方案是将其抽象化,并强制每个项目对其进行扩展,但我怀疑有更好的方式来处理事件或其他事情 以下是websecurityConfigureAdapter @Override protected void configure(final HttpSecurity htt
websecurityConfigureAdapter
。问题是,我希望每个项目都有自定义的控制器安全性,其他一切都保持不变。最明显的解决方案是将其抽象化,并强制每个项目对其进行扩展,但我怀疑有更好的方式来处理事件或其他事情
以下是websecurityConfigureAdapter
@Override
protected void configure(final HttpSecurity http) throws Exception {
...
http.authorizeRequests()
.antMatchers("/health*").permitAll()
.antMatchers("/endpoints/**").permitAll()
.antMatchers("/rest/open/**").permitAll()
.antMatchers("/login/impersonate*").hasAnyRole("ADMIN", "ADMINISTRATOR")
// AT THIS POINT I WOULD LIKE EACH PROJECT TO OPTIONALLY CONFIGURE http AS THEY WISH
http.authorizeRequests().antMatchers("/**").authenticated();
...
}
在春季,有没有一种很酷的方法可以通过bean配置或其他方式来实现这一点
@Bean //something like this perhaps????
public void configureSecurity(final HttpSecurity http) {
http.authorizeRequests()
.antMatchers("/rest/admin*").hasAnyRole("ADMIN", "ADMINISTRATOR")
}
您可以使用多个
websecurityConfigureAdapter
类,只需确保每个类都在Springboot自动配置扫描的包中
如果客户端项目要覆盖现有的安全约束,请添加@Order
注释:
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ClientSecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(HttpSecurity http) {
http.antMatcher("/rest/admin*").authorizeRequests().anyRequest().hasAnyRole("ADMIN", "ADMINISTRATOR");
}
}
请注意authorizeRequests()之前的antMatcher(),这样做是为了限制客户端配置的范围。否则,它将删除所有默认配置(除了/rest/admin*之外的每个URL将返回403 Unauthorized)。这似乎不起作用。我可以看到两个WebSecurityConfigureAdapter都在调试模式下被命中,但是具有
@顺序(有序.最低优先级)
的那个总是释放到@顺序(有序.最高优先级)
,安全性没有被应用。这里有一个类似的问题可能会帮助您: