Turgut Dsfadfa
Turgut Dsfadfa

Reputation: 795

Spring security How to define custom role name

I have found some solutions but none of them worked for me. Below code gives

"Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [admin]" 

error. When I change auto-config attribute to "true", again gives same error.

<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>

Upvotes: 1

Views: 3918

Answers (2)

Pavel Horal
Pavel Horal

Reputation: 18194

You need to provide your own decision manager (e.g. org.springframework.security.access.vote.AffirmativeBased) with your custom configured decision voters via access-decision-manager-ref="decisionMangerId" attribute.

But be aware that having no prefix in role voter is not a good idea as it will just try to interpret all security attributes as roles. I strongly recommend to use some prefix, if not the default ROLE_.

Or you can enable expression based parsing via use-expressions="true" and use access="hasRole('admin')" expresion. When going this way, you will also need to change your IS_AUTHENTICATED_ANONYMOUSLY condition with access="permitAll".

Upvotes: 7

Jukka
Jukka

Reputation: 4663

It should read:

access="hasRole('admin')"

Upvotes: 3

Related Questions