我有一个 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/