gpt4 book ai didi

java - session 变量未在 Spring 中设置

转载 作者:行者123 更新时间:2023-11-29 09:24:13 24 4
gpt4 key购买 nike

我有一个 Spring Controller ,它将一个变量放入 session 中:

public class LoginFormController extends SimpleFormController {

public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command) throws Exception {

request.getSession().setAttribute("authenticated_user", "authenticated_user");
}
}

然后我有一个 HandlerInterceptor。在 preHandle 方法中,我检查 session 中的变量:

public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

/* first, check if the requested view even required authentication */
if (isAuthenticationRequired(request)) {

/* check to see that user is logged in */
if (null == request.getSession().getAttribute("authenticated_user")) {
forwardToLogonPage(request, response);
return false;
}
/* all is ok - pass the request on */
return true;
}
/* all is ok - pass the request on */
return true;
}

问题是似乎没有设置 session 变量,因为 request.getSession().getAttribute("authenticated_user")) 总是解析为 null。

有什么想法吗?

谢谢,Krt_马耳他

最佳答案

尝试 throw new ModelAndViewDefiningException(new ModelAndView("logonPage")) 而不是 return false

然后在您的登录页面中,在表单标记中明确包含操作,如下所示:

<form:form method="post" commandName="login" action="logonPage.htm">

关于java - session 变量未在 Spring 中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3975252/

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