Vivek Agrawal21
Vivek Agrawal21

Reputation: 332

Spring Security SWF : How to redirect to different flows based on some condition

I am using spring security along with spring web flow. The problem is that I need to redirect user to two different pages based on some condition during logging.

If the user is a first time logging user he will be redirected to firstTimeuser.jsp otherwise he will be redirected to homepage.jsp.

In db side i have a field IS_FIRST_TIME_USER which will be true for first time users. so in my LOGIN table I have id,username,password,IS_FIRST_TIME_USER fields.

In spring-security.xml I have

<http auto-config="true">
    <intercept-url pattern="/**" access="ROLE_USER" />
    <form-login    authentication-success-handler-ref="authenticationSuccessHandler"
                   login-page="/basic"
                   default-target-url="/basic1"
                   authentication-failure-url="/basic?error=true"
                   username-parameter="username"
                   password-parameter="password" />
    <logout logout-success-url="/basic?logout" />
</http>

Upvotes: 1

Views: 672

Answers (1)

Kalyan
Kalyan

Reputation: 1909

Yes it is possible by providing a custom implementation of AuthenticationSuccessHandler using attribute authentication-success-handler-ref.

For example see here

Note: When using this pattern, you shouldn't use default-target-url

Simple Implementation in your case would be something like below

@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException,    ServletException {          
        if(isFirstTimeLogin(authentication.getName())) {
            response.sendRedirect("/firstTimeuser");                
        } else {
            response.sendRedirect("/homepage");
        }
    }

    private boolean isFirstTimeLogin(String username) {
        //code to access your DAO and figure out whether this is first time login or not
            //Code to access your DAO and update the flag in database
        return true;
    }
}

Upvotes: 2

Related Questions