【问题标题】:How do I use custom roles/authorities in Spring Security?如何在 Spring Security 中使用自定义角色/权限?
【发布时间】:2010-11-02 11:32:37
【问题描述】:

在将旧版应用程序迁移到 Spring Security 时,我遇到了以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainProxy': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_filterSecurityInterceptor' while setting bean property 'filters' with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterSecurityInterceptor': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [superadmin]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)

在旧的应用程序中,有“superadmin”、“editor”、“helpdesk”等角色。但在所有 Spring Security 示例中,我只看到“ROLE_”(“ROLE_ADMIN”等)这样的角色。当我将“superadmin”重命名为“ROLE_ADMIN”并仅在配置中使用此角色时,一切正常。

不起作用:

 <http auto-config="true">                                      
    <intercept-url pattern="/restricted/**" access="superadmin"/>
    <form-login
        authentication-failure-url="/secure/loginAdmin.do?error=true"
        login-page="/secure/loginAdmin.do" />        
</http> 

作品:

<http auto-config="true">                                      
    <intercept-url pattern="/restricted/**" access="ROLE_ADMIN"/>
    <form-login
        authentication-failure-url="/secure/loginAdmin.do?error=true"
        login-page="/secure/loginAdmin.do" />        
</http> 

是否可以使用自定义角色名称?

【问题讨论】:

  • 这个question 可能会有所帮助。

标签: java spring spring-security


【解决方案1】:

您正在使用默认配置,该配置期望角色以 "ROLE_" 前缀开头。您必须添加自定义安全配置并将rolePrefix 设置为“”;

http://forum.springsource.org/archive/index.php/t-53485.html

【讨论】:

  • 我可以有 ROLE_FOO 或 ROLE_BAR 或 ROLE_ANYTHING_I_WANT 吗?您提供的链接没有解决问题,只是有人说“无法为 RoleVoter 设置 RolePrefix”
  • 是的,你可以拥有任何你想要的角色。该链接显示了一个配置示例,它可能对您没有帮助,但对 D. Wroblewski 很有用。如果您需要更多帮助,只需发布​​一个新问题,很多人都愿意回答。
【解决方案2】:

这是使用访问表达式的完整配置(@rodrigoap 提供的链接似乎有点过时了):

<http
        access-decision-manager-ref="accessDecisionManager"
        use-expressions="true">

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
    <beans:property name="decisionVoters">
        <beans:list>
            <beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/>
            <beans:bean class="org.springframework.security.access.vote.RoleVoter">
                <beans:property name="rolePrefix" value=""/>
            </beans:bean>
            <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
        </beans:list>
    </beans:property>
</beans:bean>

【讨论】:

    【解决方案3】:

    您也可以始终使用表达式(通过配置use-expressions="true")来忽略ROLE_ 前缀。

    阅读Spring Security 3.1源码后发现use-expressions="true"时:

    对于&lt;security:http &gt;:
    HttpConfigurationBuilder#createFilterSecurityInterceptor() 将注册WebExpressionVoter 但不会注册RoleVoterAuthenticatedVoter;

    对于&lt;security:global-method-security &gt;GlobalMethodSecurityBeanDefinitionParser#registerAccessManager() 将注册PreInvocationAuthorizationAdviceVoter(有条件地),然后总是注册RoleVoterAuthenticatedVoter,有条件地注册Jsr250Voter

    PreInvocationAuthorizationAdviceVoter 将处理根据@PreAuthorize 生成的PreInvocationAttribute(PreInvocationExpressionAttribute 将作为实现)。 PreInvocationExpressionAttribute#getAttribute()总是返回null,所以RoleVoterAuthenticatedVoter不要投票。

    【讨论】:

      【解决方案4】:

      使用 Spring Security 3.2,这对我有用。

      更改角色前缀:

      <beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
          <beans:property name="rolePrefix" value="NEW_PREFIX_"/>
      </beans:bean>
      
      <beans:bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter"/>   
      
      <beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
          <beans:constructor-arg >
              <beans:list>
                  <beans:ref bean="roleVoter"/>
                  <beans:ref bean="authenticatedVoter"/>
              </beans:list>
          </beans:constructor-arg>
      </beans:bean>
      

      根据您要在何处应用角色前缀,可以在安全模式级别或 bean 级别应用它。

      <http access-decision-manager-ref="accessDecisionManager" use-expressions="true">
      

      在服务级别应用角色前缀:

      <beans:bean id="myService" class="com.security.test">
          <security:intercept-methods  access-decision-manager-ref="accessDecisionManager">
              <security:protect access="NEW_PREFIX_ADMIN"/>
          </security:intercept-methods>
      </beans:bean>
      

      【讨论】:

        【解决方案5】:

        这也可能有帮助:

        http://forum.springsource.org/showthread.php?96391-Spring-Security-Plug-in-ROLE_-prefix-mandatory

        基本上,它说你必须在 grails-app/conf/spring/resources.groovy 中编写:

        roleVoter(org.springframework.security.access.vote.RoleVoter) {
            rolePrefix = ''
        }
        

        它对我有用。

        【讨论】:

          猜你喜欢
          • 2020-10-14
          • 2019-12-10
          • 2011-12-24
          • 2018-12-30
          • 1970-01-01
          • 2012-10-24
          • 2022-08-09
          • 2011-09-15
          • 2012-09-01
          相关资源
          最近更新 更多