为什么会话超时后websocket连接没有终止?

ct3nt3jp  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(377)

我的决定是:
我用 STOMP CONNECT frame header 验证而不是 cookie-based authentication 因为移动应用程序没有很好的 Cookie-based authentication 支持。
我用 X-Auth-Token 要获取的标题 Authentication 因为 WebSocket Protocol 不允许javascript更改 HTTP headers 在http握手期间。默认情况下,spring security使用 Cookie-based authentication spring安全中的身份验证非常简单。
httpsessionconfig.java文件

  1. // see: https://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession-rest
  2. @Configuration
  3. //// Override HttpSession's Filter, in this instance Spring Session is backed by Redis.
  4. //@EnableRedisHttpSession
  5. public class HttpSessionConfig {
  6. // // Default connection configuration, to localhost:6739.
  7. // @Bean
  8. // public LettuceConnectionFactory connectionFactory() {
  9. // return new LettuceConnectionFactory();
  10. // }
  11. // Tell Spring to use HTTP headers, X-Auth-Token.
  12. @Bean
  13. public HttpSessionIdResolver httpSessionIdResolver() {
  14. return HeaderHttpSessionIdResolver.xAuthToken();
  15. }
  16. }

这个 STOMP CONNECT frame header 验证逻辑:
我明白了 String sessionId = ...X-Auth-Token 标题的值。
我明白了 Session session = ...sessionRepository.findById(sessionId) 我明白了 SecurityContextImpl securityContext = ...session.getAttribute("SPRING_SECURITY_CONTEXT") 我明白了 Authentication user = ...securityContext.getAuthentication() .
最后,我设置了 StompHeaderAccessor accessoraccessor.setUser(user) .
应用程序.yml

  1. server.servlet.session.timeout: 1m

websocketconfig.java文件

  1. @Configuration
  2. // see: https://docs.spring.io/spring-session/docs/current/reference/html5/#websocket-usage
  3. @EnableScheduling
  4. @EnableWebSocketMessageBroker
  5. @Order(Ordered.HIGHEST_PRECEDENCE + 99)
  6. public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> {
  7. private final SessionRepository<? extends Session> sessionRepository;
  8. public WebSocketConfig(SessionRepository<? extends Session> sessionRepository) {
  9. this.sessionRepository = sessionRepository;
  10. }
  11. // see: https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket-stomp-authentication-token-based
  12. @Override
  13. public void configureClientInboundChannel(ChannelRegistration registration) {
  14. registration.interceptors(new ChannelInterceptor() {
  15. @Override
  16. public Message<?> preSend(Message<?> message, MessageChannel channel) {
  17. StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
  18. if (StompCommand.CONNECT.equals(accessor.getCommand())) {
  19. String sessionId = accessor.getFirstNativeHeader("X-Auth-Token");
  20. Session session = sessionRepository.findById(sessionId);
  21. if (session != null) {
  22. SecurityContextImpl securityContext = session.getAttribute("SPRING_SECURITY_CONTEXT");
  23. Authentication user = securityContext.getAuthentication();
  24. accessor.setUser(user);
  25. }
  26. }
  27. return message;
  28. }
  29. });
  30. }
  31. @Override
  32. protected void configureStompEndpoints(StompEndpointRegistry registry) {
  33. registry.addEndpoint("/chat");
  34. }
  35. @Override
  36. public void configureMessageBroker(MessageBrokerRegistry registry) {
  37. registry.setApplicationDestinationPrefixes("/app");
  38. registry.enableSimpleBroker("/topic", "/queue");
  39. registry.setUserDestinationPrefix("/user");
  40. }
  41. }

我的预期结果是:根据 Spring 会议的正式文件。如果设置 server.servlet.session.timeout: 1mapplication.yml 我的实际结果是:websocket连接没有终止,用户仍然订阅了 /topic/channel/{channelId} 用户仍然可以向 /app/channel/{channelId} .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题