【问题标题】:Using custom method security annotation in spring security在spring security中使用自定义方法安全注解
【发布时间】:2016-05-18 17:21:32
【问题描述】:

我想使用自定义注释标记类中的方法,该注释将使用 Spring Security 控制授权决策。例如:

@Role("ADMIN")
public void accessControlledMethod(){}

我了解这意味着我需要以某种方式注册我的自定义注释“角色”,以便当AccessDecisionManager 做出授权决定时它可以导致ConfigAttributes 存在。但是,我不明白如何使用 spring security 注册我的自定义注释以便识别它。

我在框架代码中看到了一种潜在的解决方案。有一个名为 SecuredAnnotationSecurityMetadataSource 的类,其文档说“为自定义注释注入 AnnotationMetadataExtractor”。如果这是首选方法,我不确定如何配置 SecuredAnnotationSecurityMetadataSource 或如何将 AnnotationMetadataExtractor 注入其中。

【问题讨论】:

  • 您使用自定义注解而不是 Spring Security 提供的注解是否有原因?
  • @XtremeBiker 我的团队有一个现有的授权框架,该框架使用特定于我们应用程序的不同命名约定。我想在我们的方法安全注释中使用这些角色名称。方法文档清楚地表明这是受某种支持的,但我找不到任何关于它如何工作的信息。

标签: java spring spring-security annotations


【解决方案1】:

您可以在您的配置中扩展GlobalMethodSecurityConfiguration

@EnableGlobalMethodSecurity
@Configuration
public class MyMethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    protected MethodSecurityMetadataSource customMethodSecurityMetadataSource() {
        return SecuredAnnotationSecurityMetadataSource(...);
    }    
}

在 xml 中,您可以:

<global-method-security metadata-source-ref="customMethodSecurityMetadataSource">
...
</global-method-security>
<bean id="customMethodSecurityMetadataSource"  class="org.springframework.security.access.annotation.SecuredAnnotationSecurityMetadataSource">
...
</bean>

customMethodSecurityMetadataSource 可以是 MethodSecurityMetadataSource 的任何实例

【讨论】:

  • 感谢您的回复。我目前正在使用 XML 配置,是否有等效的 XML 配置?
  • 谢谢,我不小心列出了错误的 XSD,它说不支持 metadata-source-ref。哎呀。
  • 另外,我应该注意这个 global-method-security 配置应该在 servlet.xml 配置中而不是在 security.xml 配置中。
【解决方案2】:

这在 Spring 5 中不起作用,因为默认 bean 覆盖默认是禁用的。它仅适用于将spring.main.allow-bean-definition-overriding 属性设置为true

如果有人知道如何在不启用 bean 覆盖的情况下将自定义 MethodSecurityMetadataSource 添加到 GlobalMethodSecurityConfiguration,这将对更新的 Spring 版本有所帮助

【讨论】:

  • 刚刚偶然发现这个问题,试试我的答案。
【解决方案3】:

在 Spring Boot 中,您可以通过覆盖 GlobalMethodSecurityConfiguration 中的相应方法并添加/修改超类中的值来添加自定义 MethodSecurityMetadataSources 和 AccessDecisionVoters。

@Configuration
@AutoConfigureAfter(SecurityConfiguration.class)
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class MethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {
    @Override
    public MethodSecurityMetadataSource methodSecurityMetadataSource() {
        var source = (DelegatingMethodSecurityMetadataSource) super.methodSecurityMetadataSource();
        source.getMethodSecurityMetadataSources().add(new FooSecurityMetadataSource());
        return source;
    }

    @Override
    protected AccessDecisionManager accessDecisionManager() {
        var manager = (AffirmativeBased) super.accessDecisionManager();
        manager.getDecisionVoters().add(new FooVoter());
        return manager;
    }
}

【讨论】:

  • 实现customMethodSecurityMetadataSource() 方法的作用完全相同,只是它不依赖于当前的实现。所以如果你问我,这不是一个很好的答案。
  • 是的,你可能是对的。不知道为什么我当时没有看到这个。
猜你喜欢
  • 1970-01-01
  • 2013-05-24
  • 2012-03-03
  • 2017-01-11
  • 2014-10-18
  • 2016-08-07
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多