我正在使用Spring Security和Spring Web Flow。问题是我需要在登录期间根据某种条件将用户重定向到两个不同的页面。
如果该用户是首次登录的用户,则将其重定向到firstTimeuser.jsp,否则将重定向到homepage.jsp。
在数据库端,我有一个IS_FIRST_TIME_USER字段,对于首次使用的用户而言将为true。所以在我的登录表中,我有ID,用户名,密码,IS_FIRST_TIME_USER字段。
在spring-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>
是的,可以通过使用attribute提供AuthenticationSuccessHandler的自定义实现来实现authentication-success-handler-ref
。
例如看这里
注意:使用此模式时,请勿使用 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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句