【问题标题】:Multiple @EnableGlobalMethodSecurity annotations多个 @EnableGlobalMethodSecurity 注释
【发布时间】:2016-09-08 04:03:38
【问题描述】:

如果多个配置类都有@EnableGlobalMethodSecurity注解,那么是使用一个而忽略一个?

在 Spring Boot 应用程序中,有两个 WebSecurityConfigurerAdapter 实例 - 一个带有 @EnableGlobalMethodSecurity(secured = true) 注释,另一个带有 @EnableGlobalMethodSecurity(prePostEnabled = true) 注释。但到目前为止,我无法让 @PreAuthorize 注释工作。只有一个注释我可以看到验证它正在被应用。 例如

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class FirstConfigurer extends WebSecurityConfigurerAdapter {
...

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class AnotherConfiguration extends WebSecurityConfigurerAdapter{
...

spring-security 是否支持使用@EnableGlobalMethodSecurity 注解的多个配置类?
有没有办法查看实际配置了什么?

【问题讨论】:

    标签: spring-security spring-boot


    【解决方案1】:

    我调试它(​​进入 springframework 源)以查看它正在加载什么以及按什么顺序。

    我有一个类似的情况,我有一个配置 @EnableGlobalMethodSecurity(securedEnabled = true) 的库(我无法更改)。

    我想使用@EnableGlobalMethodSecurity(prePostEnabled=true)

    发生了什么是库配置被加载到我的配置上(只会使用最后一个加载的配置)

    我尝试使用@Order(Ordered.LOWEST_PRECEDENCE)@EnableGlobalMethodSecurity(order = Ordered.HIGHEST_PRECEDENCE, ... 但没有效果。

    我的解决方案是通过在我的配置中导入库配置来设置我想要的顺序,如下所示:

    @Import(LibraryConfig.class)
    @EnableGlobalMethodSecurity(prePostEnabled=true)
    public class MyConfiguration extends GlobalMethodSecurityConfiguration {
        /* my code */
    }
    

    PLUS:我发现我的情况更加复杂,因为我使用的库不止一次配置@EnableGlobalMethodSecurity(securedEnabled = true)。 所以行为有点不同,为了强制使用我的配置,我必须重写这些方法的实现:

    @Bean
    public MethodSecurityMetadataSource methodSecurityMetadataSource() {
        return super.methodSecurityMetadataSource();
    }
    
    @Bean
    public MethodInterceptor methodSecurityInterceptor() throws Exception {
        return super.methodSecurityInterceptor();
    }
    
    @Bean
    public PreInvocationAuthorizationAdvice preInvocationAuthorizationAdvice() {
        return super.preInvocationAuthorizationAdvice();
    }
    

    【讨论】:

    • 在我们的一款产品上遇到了同样的问题,这有助于解决问题。
    • 在我的安全配置中,它具有 ©EnableGlobalMethodSecurity,以及另一个在 MethodSecurityConfig 的不同配置中用于 ©PreAuthroize 中的自定义方法。正如你所说,我从 MethodSecurityExpressionHandler 中删除了 EnableGlobalMethodSecurity 并添加了 ©Import(MethodSecurityConfig.class),但我仍然收到错误消息。我做对了吗?与其同时保留它,还需要将其从其中一个中删除并在另一个中添加 ©Import 中的配置?
    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多