gpt4 book ai didi

java - 使用 Spring Security 登录后无法重定向到不同页面

转载 作者:行者123 更新时间:2023-12-02 12:00:53 26 4
gpt4 key购买 nike

我正在使用 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");

关于java - 使用 Spring Security 登录后无法重定向到不同页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47260189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com