2014-05-25 34 views
1

我正在使用弹簧安全和弹簧web流程。问题是我需要根据日志记录中的某些条件将用户重定向到两个不同的页面。Spring Security SWF:如何根据一些条件重定向到不同的流程

如果用户是第一次登录用户,他将被重定向到firstTimeuser.jsp,否则他将被重定向到homepage.jsp。

在数据库方面我有一个字段IS_FIRST_TIME_USER这将是真正的第一次的用户。 所以在我的登录表中我有ID,用户名,密码,IS_FIRST_TIME_USER字段。

在弹簧的security.xml我

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

回答

1

是的,它可以通过提供使用属性authentication-success-handler-ref AuthenticationSuccessHandler的自定义实现。

例如看到here

注意:如果使用这种模式,你不应该在链接中使用default-target-url

你的情况简单的实现方式是类似下面

@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; 
    } 
} 
+0

的例子解释了如何根据ROLES重定向到不同的流程。但是我对这个角色没有任何问题。我需要MySimpleUrlAuthenticationSuccessHandler类中某处的IS_FIRST_TIME_USER属性。根据IS_FIRST_TIME_USER的值,我想选择一个流程。 –

+0

我用一个简单的实现更新了答案。请检查是否适合您。 – Kalyan

相关问题