【问题标题】:Allowing a client application to configure http security from an already wired WebSecurityConfigurerAdapter允许客户端应用程序从已连接的 WebSecurityConfigurerAdapter 配置 http 安全性
【发布时间】:2016-02-09 17:17:19
【问题描述】:

我正在使用 spring-boot 和 spring-security。

我有一个通用的WebSecurityConfigurerAdapter 设置用于许多不同的项目。问题是我想要每个项目的自定义控制器安全性,其他一切都保持不变。最明显的解决方案是将其抽象化并强制每个项目对其进行扩展,但我怀疑有更好的方法来处理事件或其他东西。

这里是WebSecurityConfigurerAdapter的配置方法

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

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    你可以使用多个WebSecurityConfigurerAdapter类,只要确保它们都在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)。

    【讨论】:

    • 这似乎不起作用。我可以看到两个 WebSecurityConfigurerAdapters 在调试模式下都被击中,但带有@Order(Ordered.LOWEST_PRECEDENCE) 的那个总是丢失到@Order(Ordered.HIGHEST_PRECEDENCE) 并且没有应用安全性。
    • 这里有一个类似的问题可能会对您有所帮助:stackoverflow.com/questions/18815015/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多