【问题标题】:Spring Stomp SimpUserRegistry Anonymous UsersSpring Stomp SimpUserRegistry 匿名用户
【发布时间】:2019-03-04 12:41:05
【问题描述】:

SimpUserRegistry 让您检索所有经过身份验证的 Stomp 会话的详细信息,是否有任何此类可以让我遍历匿名用户会话?

【问题讨论】:

  • 您的应用程序如何拥有未经身份验证的会话?你的意思是匿名用户?
  • 是的,我的意思是,对不起
  • 您能指定您使用的是哪个 spring-framework/spring boot 版本吗?
  • @FlorianDe 2.1.3.RELEASE 弹簧靴

标签: java spring spring-boot websocket stomp


【解决方案1】:

就像他的回答中描述的howie,只有非匿名用户才会被添加到SimpUserRegistry。

但如果你真的想添加匿名用户,你只需要继承 DefaultHandshakeHandler 类并重写如Spring Doc for Version 5.0.0.M1 - Chapter 22 中所述的determineUser 方法。这也应该适用于您在使用 Spring Boot 2.1.3.RELEASE 时当前使用的 5.1.5.Release:

在某些情况下,即使用户尚未经过正式身份验证,为 WebSocket 会话分配身份也可能很有用。例如,移动应用程序可能会根据地理位置为匿名用户分配一些身份。目前,应用程序可以继承 DefaultHandshakeHandler 并覆盖 determineUser 方法。然后可以插入自定义握手处理程序(请参阅第 22.2.4 节“部署注意事项”中的示例)。

这是一个答案 (Spring websockets without principal),它向您展示了如何创建 AnonymousPrincipal 并在自定义握手处理程序中确定它。

最后,您必须将自定义握手处理程序的实例添加到您注册的端点,但这取决于您是否使用 STOMP。

【讨论】:

    【解决方案2】:

    以下是 StompSubProtocolHandler 的一些代码 sn-ps - handleMessageFromClient 方法将用户添加到 stompAuthentications 映射并发布 SessionConnectEvent 事件 -

    public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) {
    //...
    SimpAttributesContextHolder.setAttributesFromMessage(message);
    boolean sent = outputChannel.send(message);
    
    if (sent) {
        if (isConnect) {
            Principal user = headerAccessor.getUser();
            if (user != null && user != session.getPrincipal()) {
                this.stompAuthentications.put(session.getId(), user);
            }else{
                  //TODO try to handle here for anonymous user
            }
        }
        if (this.eventPublisher != null) {
            if (isConnect) {
                publishEvent(new SessionConnectEvent(this, message, getUser(session)));
            }
    //...
    

    我认为您必须检查此安全代码StompSubProtocolHandler,并对其进行自定义。

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2014-05-16
      • 1970-01-01
      • 2020-08-19
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      相关资源
      最近更新 更多