如何获取序列化的tomcat HttpSession来重新填充spring SessionRegistry

时间:2017-09-26 17:12:27

标签: java spring tomcat spring-security httpsession

我执行以下操作:

  1. 登录我的春季应用程序(例如,以用户' admin')
  2. 停止tomcat
  3. 现在我看到会话被序列化为 sessions.ser 文件
  4. 我重启tomcat
  5. sessions.ser文件消失了(我想它在服务器启动时被反序列化了?)
  6. 现在我发送一个需要登录用户的请求(完成后) 在步骤1中,即我在之前已经登录的浏览器中点击F5,所以我猜请求沿着jSessionId等发送。)
  7. 调试时,我可以观察spring如何在
  8. 中成功加载SecurityContext

    HttpSessionSecurityContextRepository.readSecurityContextFromSession

    使用存储的Session-Information(它是一个UsernamePasswordAuthenticationToken包含一个包含Principal等的Authentication对象,因此可以获得自定义User对象)

    1. Spring接受请求,即无需重新登录并发送适当的响应
    2. 但是,尝试使用SessionRegistry通过

      列出登录用户时
      for (Object principal : sessionRegistry.getAllPrincipals()) {
                  MyCustomUser myCustomUser = (MyCustomUser) principal;
                  ClientQueryDetails client = clientQuery.getDetails(myCustomUser 
                          .getClientId()).get();
                  List<SessionInformation> sessions = sessionRegistry.getAllSessions(
                          principal, false);
                  for (SessionInformation sessionInformation : sessions) {
                      result.add(new SessionInfo(client.getName(), myCustomUser 
                              .getUsername(), sessionInformation.getSessionId(),
                              sessionInformation.getLastRequest()));
                  }
              }
      

      正如我通常所做的那样,可视化当前有效的用户/会话,为空

      为什么Spring此时不会将这些Principal添加到SessionRegistry? 可以/我应该以某种方式手动完成吗?

      我读过https://github.com/spring-projects/spring-security/issues/2062这听起来像这样做是个坏主意。

      似乎还有Getting logged in users with sessionRegistry not work when manually authenticate

      我还找到了http://forum.spring.io/forum/spring-projects/web/71503-spring-not-restoring-persistent-sessions-to-session-registry

      总结一下我的问题:

      1. 为什么不重新将序列化的会话信息添加到SessionRegistry
      2. 是否正在查询SessionRegistry以便向用户(即登录用户)显示所有活动的会话以正确的方式执行此操作? 的修改 是的,这绝对是SessionRegisty的目的:https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#list-authenticated-principals
      3. 我应该手动将Principal添加到SessionRegistry吗? 的修改 https://github.com/spring-projects/spring-security/issues/2062提供了手动读取SessionRegistry会话的不同方法,但似乎有一些注意事项。

      4. 来自 sessions.ser 的会话在何处以及如何被反序列化以及Spring获取它的位置?或者换句话说,会话信息如何从sessions.ser文件进入春天的SecurityContext?特别是如何移交&#34;从tomcat到spring?

1 个答案:

答案 0 :(得分:0)

我在SessionRegistry中没有看到Sessions但是拥有有效Sessions(即登录用户)的问题的解决方案是,只需删除服务器重启时的SESSIONS.ser文件

因此,所有用户都必须再次登录,并相应地填充SessionRegistry。由于我没有迫切需要保持会议活着,这对我来说是一个很好的解决方案。