![](/img/trans.png)
[英]Redirect to different page after login based on user role with Spring Security
[英]Cannot redirect to different pages after Login with Spring Security
我正在使用Spring Security登錄到我的應用程序,並且嘗試登錄后重定向到某些頁面,但是我無法連接,我認為這是spring-security.xml
的問題,這是我的代碼:
MyAuthenticationSuccessHandler :
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
...
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
...
// check if user is active after successfully logging in
if (!isActiveStatus(authentication)) {
logger.info("User [" + username + "] is not longer active.");
authentication.setAuthenticated(false);
response.sendRedirect(request.getContextPath() + "/home/welcome?notactive");
return;
}
...
}
...
}
spring-security.xml :
....
<security:intercept-url pattern="/home/**" access="permitAll" />
<security:intercept-url pattern="/" access="permitAll" />
<security:intercept-url pattern="/**" access="isAuthenticated()" />
<security:form-login login-page="/home/welcome?login"
default-target-url="/home/welcome"
authentication-success-handler-ref="MyAuthenticationSuccessHandler"
authentication-failure-handler-ref="MyAuthenticationFailureHandler" />
....
使用此代碼,應用程序將始終重定向到/home/welcome?login
而永遠不會重定向到/home/welcome?notactive
。 任何想法?
提前致謝。
您似乎正在嘗試手動發送重定向,SuccessHandler具有它自己的機制:
getRedirectStrategy().sendRedirect(request, response, "/home/welcome?notactive");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.