没有主体的Spring WebSockets [英] Spring websockets without principal

查看:121
本文介绍了没有主体的Spring WebSockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个简单的websockets应用程序,该应用程序可以将消息从一个端点发送到在其他地方建立的指定会话.到目前为止,我已经能够使用批注@SendToUser()来使客户端订阅频道(如在此问题中所讨论的:

I am trying to implement a simple websockets application that can send messages from one endpoint to a specified session established elsewhere. So far I have been able to use the annotation @SendToUser() in order for a client to subscribe to a channel (as discussed in this question: Spring Websockets @SendToUser without login?)

但是,我现在想创建一个单独的端点,该端点在被调用时查找与传递到该端点的数据相关联的用户,并向该用户发送有关此数据的消息.

However, I now want to create a separate endpoint that, when called, looks up a user associated with data passed into this endpoint and sends a message to that user regarding this data.

但是,由于我的用户没有Principal(我没有使用Spring Security),所以我无法准确地确定如何使其能够调用SimpMessagingTemplate convertAndSendToUser()命令.

However, I have not been able to accurately determine how to make it so that I can call SimpMessagingTemplate convertAndSendToUser() command because my users do not have a Principal (I'm not using Spring Security).

我已经能够从传递到@MessageMapping端点的MessageHeaders中获取simpSessionId,但是现在我不知道如何使用simpSessionId从应用程序的不同部分向下发送信息.

I've been able to get the simpSessionId from the MessageHeaders passed in to a @MessageMapping endpoint, but now I can't figure out how to use the simpSessionId to send info down from different parts of my application.

我已经进行了一些有关重写DefaultHandshakeHandler的defineUser()方法并在成功进行Websocket握手后将随机生成的UUID作为用户名分配给用户的研究(如对此问题的回答中所述:

I've done some research regarding overriding the determineUser() method of DefaultHandshakeHandler and assigning a randomly generated UUID as the username to a user upon successful websocket handshake (as described in an answer to this question: How to reply to unauthenticated user in Spring 4 STOMP over WebSocket configuration?), but since the principal coming up is null I'm not exactly sure how to properly generate one and assign it to the Principal for use with the application.

我基本上需要具有匿名用户的能力,并在他们创建Websocket连接后从应用程序的不同部分向他们发送消息.

I basically need the ability to have anonymous users and send them messages from different parts of the application after they've created a websocket connection.

推荐答案

所以对于其他面临类似问题的人,我实现了自己的Principal类:

So for anyone else facing a similar problem, I implemented my own Principal class:

package hello;

import java.security.Principal;
import java.util.Objects;

public class AnonymousPrincipal implements Principal {

    private String name;

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object another) {
        if (!(another instanceof Principal))
            return false;

        Principal principal = (Principal) another;
        return principal.getName() == this.name;

    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

然后,我实现了自己的DefaultHandshakeHandler版本:

Then, I implemented my own version of DefaultHandshakeHandler:

package hello;

import java.security.Principal;
import java.util.Map;
import java.util.UUID;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

public class CustomHandshakeHandler extends DefaultHandshakeHandler {

    @Override
    protected Principal determineUser(ServerHttpRequest request,
            WebSocketHandler wsHandler, Map<String, Object> attributes) {
        Principal principal = request.getPrincipal();           

        if (principal == null) {
            principal = new AnonymousPrincipal();

            String uniqueName = UUID.randomUUID().toString();

            ((AnonymousPrincipal) principal).setName(uniqueName);
        }

        return principal;

    }

}

现在,Websocket会话会在握手发生时将此主体分配给它,因此,如果用户是匿名的,他们将得到一个匿名主体分配,这将允许我存储其名称(生成的UUID),以便以后在其他部分使用应用程序.

Now a websocket session gets this principal assigned to it when the Handshake takes place so if the user is anonymous they get an anonymous principal assigned that will allow me to store their name (generated UUID) for later use in other parts of the application.

这篇关于没有主体的Spring WebSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆