繁体   English   中英

如何获取序列化的tomcat HttpSession以重新填充Spring SessionRegistry

[英]How to get serialized tomcat HttpSession to repopulate spring SessionRegistry

我执行以下操作:

  1. 登录到我的spring应用程序(例如,以“ admin”用户身份)
  2. 停止tomcat
  3. 现在,我看到该会话已序列化为sessions.ser文件
  4. 我重启tomcat
  5. sessions.ser文件消失(我猜它在服务器启动期间正在反序列化吗?)
  6. 现在,我发送一个需要登录用户的请求(如步骤1所示,即我在之前已经登录过的浏览器中点击了F5,所以我猜该请求会随jSessionId等一起发送)
  7. 调试时,我可以观察spring如何成功地将SecurityContext加载到

HttpSessionSecurityContextRepository.readSecurityContextFromSession

与存储的会话信息(它是一个UsernamePasswordAuthenticationToken,包含一个包含Principal等的Authentication对象,因此获得自定义User对象似乎是可能的)

  1. Spring接受请求,即,无需重新登录,并且发送适当的响应

但是,当尝试通过以下方式使用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此时不将这些主体添加到SessionRegistry中? 我可以/应该以某种方式手动进行吗?

我已经阅读了https://github.com/spring-projects/spring-security/issues/2062 ,听起来像是个坏主意。

似乎也相关,手动进行身份验证时,使用sessionRegistry登录的用户无法正常工作

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

所以总结一下我的问题:

  1. 为什么不将序列化的会话信息重新添加到SessionRegistry呢?
  2. 查询SessionRegistry以便向用户( 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. 在哪里以及如何exactely从sessions.ser会话被反序列化为以及哪里春天获得它? 换句话说,会话信息如何从sessions.ser文件获取到spring的SecurityContext中? 特别是它是如何从雄猫“移交给”春天的?

对于在SessionRegistry中看不到Session却具有有效Session(即已登录用户)的问题,我的解决方法是在服务器重启时简单地删除SESSIONS.ser文件

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM