【发布时间】:2021-01-10 00:45:05
【问题描述】:
我有这个控制器
@Controller
@RequestMapping(value = "/login")
public class Login{
@RequestMapping
public ModelAndView mainPage(HttpServletRequest request){
request.getSession().setAttribute("testSession", "Session test");
return new ModelAndView("/login");
}
@RequestMapping(value = "/check")
public View check(HttpServletRequest request){
System.out.println(request.getSession(false)); //null
return new RedirectView("/login");
}
}
当访问/login 时,我创建一个会话并在其中添加一个属性"testSession":request.getSession().setAttribute("testSession", "Session test");
在页面/login 上有这个<form action="/login/check" method="post">。
在/login/check 上,我尝试在/login 上创建会话,但它为空。
为什么会话在请求之间不存在?
P.S.:我的应用在远程服务器上运行,Tomcat 和 Apache 作为反向代理,我通过 https://mydom.com 访问我的应用
更新
我创建了一个控制器来测试会话:
@Controller
@RequestMapping(value = "/sess")
public class TestSession{
@RequestMapping(method = RequestMethod.GET)
public void mainPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
//get or create session
HttpSession session = request.getSession();
response.setContentType("text/html");
response.getWriter().println(session.getId());
}
}
对/sess 的每个请求都会打印另一个ID。
【问题讨论】:
-
你如何测试这个?确保将会话 ID 从客户端发送回服务器。
-
@M.Deinum 究竟如何?我是 spring/web 的新手
-
这与 Spring 无关,而与您如何测试它有关。如果您没有发送 sessionId cookie(假设您正在使用 cookie),那么就没有什么可以恢复的了。还要确保你已经正确配置了 Spring Session。
-
@M.Deinum 我们可以在这里谈谈chat.stackoverflow.com/rooms/195135/… 吗?我觉得我缺少信息。
-
只需解释(或展示)您是如何测试它的。您正在讨论页面,哪些页面显示您是如何做事的。还要添加你的 Spring Session 配置(应该配置一个过滤器)。