出于记录目的,我想创建一个记录器,自动将当前会话的 ID 添加到记录的行中。
对于登录用户,这不是问题:
((WebAuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails())
.getSessionId()
问题是,在用户登录之前 getAuthentication()
返回 null
。有没有另一种方法可以在不引用当前响应或任何类似内容的情况下获取会话 ID?
原文由 abyx 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以使用
这依赖于 Spring 的
RequestContextHolder
,所以它应该与 Spring MVC 的DispatcherServlet
一起使用,或者你应该声明RequestContextListener
如果不存在,也会创建会话。