【发布时间】:2014-09-17 11:58:36
【问题描述】:
在 Spring MVC 4 应用程序中通过 websocket 连接发送的调用中,当将其作为参数添加到方法中时,我可以获得 java.security.Principal 对象。
但是,这基本上只有用户名,我需要在登录时创建的(扩展的)UserDetails 对象。
SecurityContextHolder.getContext().getAuthentication() 返回null。据我了解,因为 spring 创建了一个特殊的会话来处理 websocket 连接。
问题:有没有办法从 websocket 会话/线程内部访问安全上下文?
(能够访问会话 bean 也足够了)。
【问题讨论】:
-
对于其他人:我使用 Spring Session 解决了这个问题。 projects.spring.io/spring-session。根本不喜欢这个解决方案,因为它需要我保留 websocketsessions 和 httpsession 之间的映射。感觉丑陋和不安全,但我必须让它工作。将保持这个问题开放,以便获得更优雅的解决方案。
标签: spring-mvc spring-security websocket spring-websocket