How do you test roles?
If you define them in your security context as follows:
<intercept-url pattern="/adminStuff.html**" access="hasRole('ROLE_ADMIN')" />
You can set defaultFailureUrl
to SimpleUrlAuthenticationFailureHandler
, and when a less privileged user tries to access the secure URL, FaliureHandler
should redirect you to defaultFailureUrl
, which may be your login page.
You can enter FaliureHandler
in filter at position FORM_LOGIN_FILTER
.
<bean id="myFaliureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <property name="defaultFailureUrl" value="http://yourdomain.com/your-login.html"/> </bean> <bean id="myFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="authenticationFailureHandler" ref="myFaliureHandler"/> </bean> <http> <custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" /> </http>
Answer 1) in the comment.
This will be a little more than I thought, given your namespace configuration.
What you need to do is remove the <form-login>
definition and add "custom" UsernamePasswordAuthenticationFilter
instead (this is the filter that processes the <form-login>
element).
You also need to remove <access-denied-handler>
.
Thus, your configuration will look something like this:
<bean id="myFaliureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <property name="defaultFailureUrl" value="http://yourdomain.com/your-login.html"/> </bean> <bean id="myFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="authenticationFailureHandler" ref="myFaliureHandler"/> </bean> <bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <property name="loginFormUrl" value="/login"/> </bean> <http entry-point-ref="authenticationEntryPoint" auto-config="false"> <custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" /> </http>
As a rule, also see spring docs on custom filters , if you have not already done so. We are currently using this config in my current company, forcing users to switch if privileges are not required on the page.
source share