【问题标题】:Spring Security add/remove antMatchers and roles dynamically at runtimeSpring Security 在运行时动态添加/删除 antMatchers 和角色
【发布时间】:2021-11-23 21:21:06
【问题描述】:

有没有办法动态改变这个地方?换句话说,调用添加或删除 antMatchers 或完全覆盖的方法。地图角色等。

@EnableWebSecurity
public class WebSecurityConfigAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //Change this configs dynamically at runtime
    }
    
}

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    我最终得到了这个solution。解决方案是关闭当前上下文并运行新上下文。当然,它有缺点,因为它会导致停机,但我使用负载均衡器和几个节点,所以对我来说没问题。

    【讨论】:

      【解决方案2】:

      在 Spring Security 版本 5.6 中,目前在 5.6.0.M3 中,您可以创建一个 AuthorizationManager bean 并定义将您的规则放置在您想要的任何位置,如下所示:

      @Autowired
      private MyCustomAuthorizationManager access;
      
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().access(access);
      }
      

      或者更好的是,您可以定义一个SecurityFilterChain bean 并利用方法参数注入,而不是扩展WebSecurityConfigurerAdapter

      @Bean
      SecurityFilterChain app(HttpSecurity http, MyCustomAuthorizationManager access) throws Exception {
           ...
           http.authorizeRequests().access(access);
           ...
           return http.build();
      }
      

      有一个great presentation 显示如何执行此操作。

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 2013-12-30
        • 2017-05-18
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 2018-06-02
        相关资源
        最近更新 更多