Spring Security conditional default-target-url

Viewed 19398

I've noticed that there are a couple of questions asking about this topic. I looked through them and I was unable to apply them to my specific Spring setup. I would like to configure my login redirect to be conditional, based on the user's role. This is what I have so far:

<http auto-config="true" use-expressions="true">
        <custom-filter ref="filterSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR"/>
        <access-denied-handler ref="accessDeniedHandler"/>
        <form-login
            login-page="/login"
            default-target-url="/admin/index"
            authentication-failure-url="/index?error=true"
            />
        <logout logout-success-url="/index" invalidate-session="true"/>
</http>

I thought this question might be in the same line as what I'm trying to do. Anyone know how I can apply it though?

EDIT 1

<bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
</bean>
<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/login.jsp"/>
</bean>

EDIT 2

Currently I do not have a class like public class Test implements AuthenticationSuccessHandler {} as shown in this example.

2 Answers

A more appropriate approach IMO is to create a class which extends SimpleUrlAuthenticationSuccessHandler, and then override its determineTargetUrl method. From the docs:

Builds the target URL according to the logic defined in the main class Javadoc.

...which sounds a little confusing, but basically you write whatever logic you need to determine the target URL, then just return it as a String.

Related