Java 如何在Spring Security中使用自定义角色/权限?
将遗留应用程序迁移到spring security时,我遇到以下异常:Java 如何在Spring Security中使用自定义角色/权限?,java,spring,spring-security,Java,Spring,Spring Security,将遗留应用程序迁移到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
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)
在旧的应用程序中有“超级管理员”、“编辑器”、“帮助台”等角色。但在所有Spring安全示例中,我只看到“角色管理”(“角色管理”等)等角色。当我将“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>
可以使用自定义角色名吗 您使用的是默认配置,该配置要求角色以
的“ROLE\uux”
前缀开头。您必须添加自定义安全配置并将rolePrefix
设置为“”
下面是使用access表达式的完整配置(由@rodrigoap提供的链接似乎有点过时):
这也可能有帮助:
基本上,它说您必须在grails app/conf/spring/resources.groovy中编写:
roleVoter(org.springframework.security.access.vote.RoleVoter) {
rolePrefix = ''
}
这对我很有用。您也可以始终使用expression(通过配置
使用expressions=“true”
)忽略角色前缀
阅读SpringSecurity3.1源代码后,我发现当使用expressions=“true”
:
对于
:
HttpConfigurationBuilder#createFilterSecurityInterceptor()
将注册WebExpressionVoter
,但不注册RoleVoter
、AuthenticatedVoter
对于
:GlobalMethodSecurityBeanDefinitionParser#registerAccessManager()
将注册PreInvocationAuthorizationAdviceVoter
(有条件地),然后始终注册RoleVoter
、AuthenticatedVoter
,有条件地注册JSR250 Voter
PreInvocationAuthorizationAdviceVoter
将处理根据@PreAuthorize
生成的PreInvocationAttribute
(PreInvocationExpressionAttribute将用作实现)PreInvocationExpressionAttribute#getAttribute()
总是返回null,所以RoleVoter
、AuthenticatedVoter
不要对它进行投票。使用Spring Security 3.2,这对我来说很有效
更改角色前缀:
根据您希望应用角色前缀的位置,可以在安全模式级别或bean级别应用角色前缀
<http access-decision-manager-ref="accessDecisionManager" use-expressions="true">
在服务级别应用角色前缀:
我可以要一个角色\u FOO或角色\u BAR或角色\u任何我想要的东西吗?你提供的链接并没有解决这个问题,只是有人说“无法为RoleVoter设置RolePrefix”是的,你可以扮演任何你想要的角色。该链接显示了一个配置示例,可能对您没有帮助,但对D.Wroblewski很有用。如果你需要更多帮助,只需发布一个新问题,很多人都准备好回答。这可能会有所帮助。
<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>
<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>