Where does the user in the springboot websocket convertAndSendToUser method come from?

I searched for it all day and found a similar answer to the google, that I was able to get home, but I don"t think I can make it clear where this user came from.
my English is not good, and I use translation software, which may be the reason why I can"t understand it

.

from-in-convertandsendtouser-works-in-sockjsspring-websocket/47956531" rel=" nofollow noreferrer "> https://stackoverflow.com/que.

so I hope someone can give me an answer

convertAndSendToUser (username, destination, message) API.
it accepts a string user name, which means that if we somehow set a unique user name for each connection, we should be able to send messages to specific users who subscribe to the topic.

what I don"t understand is, where did this user name come from?

the user name is an instance of the java.security.Principal class. But how is this Principal object obtained, how is it stored in spring, how do I make some business judgments based on this user to decide whether to send a message, and if the current user is A, send a message to B, how can this requirement be realized?


the first step is to write a class to implement Principal
for example:

public class WebsocketUserVO implements Principal {
    private  String id;
    public WebsocketUserVO(String id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return id;
    }
}

the second step is to set the user

when creating the connection
public class WebsocketUserInterceptor extends ChannelInterceptorAdapter {

    @Autowired
    private WebSocketServ webSocketServ;

    @Autowired
    private SimpUserRegistry userRegistry;

    @SuppressWarnings("rawtypes")
    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            System.out.println("success");
            Object raw = message.getHeaders().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);
            if (raw instanceof Map) {
                Object name = ((Map) raw).get("name");
                if (name instanceof LinkedList) {
                    String id = ((LinkedList) name).get(0).toString();
                    //
                    accessor.setUser(new WebsocketUserVO(id));
                    webSocketServ.pushOnlineUser(id);
                }
            }
        } else if (StompCommand.DISCONNECT.equals(accessor.getCommand())) {
            //message.getHeaders.size()=5,6size5
            System.out.println("");
            WebsocketUserVO vo = (WebsocketUserVO) message.getHeaders().get(SimpMessageHeaderAccessor.USER_HEADER);

            //  
            if (message.getHeaders().size() == 5&&StringUtils.isBlank(userRegistry.getUser(vo.getName()))) {
                webSocketServ.removeOnlineUser(vo.getName());
            }
        }
        return message;
    }

}

third step, send a message

   messagingTemplate.convertAndSendToUser(userId,url, MVCUtil.success(object));
   userIdWebsocketUserVOgetName

Menu