Spring安全性如何定义自定义角色名称

Tur*_*dfa 1 spring spring-security

我找到了一些解决方案,但没有一个能为我工作.下面的代码给出

"Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [admin]" 
Run Code Online (Sandbox Code Playgroud)

错误.当我将auto-config属性更改为"true"时,再次给出相同的错误.

<http auto-config="false">
    <intercept-url pattern="/pages/login.xhtml*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <intercept-url pattern="/**" access="admin" />
    <form-login login-page='/pages/login.xhtml' default-target-url="/**"
                authentication-failure-url="/pages/login.xhtml"/>
    <logout logout-success-url="/pages/logout.xhtml" />
</http>

<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
    <beans:property name="rolePrefix" value=""/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

Pav*_*ral 7

您需要通过属性为自己的决策经理(例如org.springframework.security.access.vote.AffirmativeBased)提供自定义配置决策选民access-decision-manager-ref="decisionMangerId".

但请注意,在角色选民中没有前缀不是一个好主意,因为它只会尝试将所有安全属性解释为角色.我强烈建议使用一些前缀,如果不是默认值ROLE_.

或者,您可以启用基于表达式的解析use-expressions="true"并使用access="hasRole('admin')"expresion.走这条路的时候,你还需要改变你的IS_AUTHENTICATED_ANONYMOUSLY状况access="permitAll".