Spring Security角色应该始终以ROLE为前缀?

时间:2013-12-26 16:00:37

标签: spring spring-security

在我们的应用程序中,我们使用的是Spring Security,我们观察到如果角色名称没有以ROLE作为前缀,则它不起作用。

我们的角色是在DB中配置的,对角色的名称没有限制。

是否有任何解决方法可以避免角色的ROLE前缀?

2 个答案:

答案 0 :(得分:1)

您可以在此处找到解决方案:Spring Security – adding a custom Role Prefix,根据您需要配置RoleVoter

<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
    <beans:property name="rolePrefix" value="" />
</beans:bean>

另见Spring Security Role Prefix and Custom User Details Service

答案 1 :(得分:1)

至于我,我没有注意到这种行为。

在我的项目中,我正在使用Spring Security 3.1.3.RELEASE和Spring 3.2.3.RELEASE。我的securityContext.xml包含以下行:

<security:http auto-config="false" use-expressions="true" access-denied-page="/denied.do"
               entry-point-ref="authenticationEntryPoint">
    <security:intercept-url pattern="/index.do" access="hasAnyRole('PROJECT_REVIEW', 'PROJECT_ADMINISTRATOR')"/>
    <!-- Skipped -->
    <security:intercept-url pattern="/**" access="hasAnyRole('PROJECT_REVIEW', 'PROJECT_ADMINISTRATOR')"/>
    <!-- Skipped -->
</security:http>

所以,我正在使用自定义角色PROJECT_REVIEW,PROJECT_ADMINISTRATOR。它工作正常。

你能告诉我你有什么错误吗?