Spring Boot Raw WebSocket 设置主体

Posted

技术标签:

【中文标题】Spring Boot Raw WebSocket 设置主体【英文标题】:Spring Boot Raw WebSocket Set Principal 【发布时间】:2019-12-04 10:29:38 【问题描述】:

我想用 Spring Boot 实时开发通知发送器,但我不能使用 Stomp 和 sockjs,所以我需要实现原始 WebSocket,但我不知道如何在 WebSocket 连接中设置 Principal,因为我想进行身份验证使用 JWT 令牌。那么,我可以在哪里或如何设置本金。 我正在使用这些;

WebSocketConfig.java:

@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer 

  @Autowired
  WebSocketNotificationSenderService senderService;

  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry
                                              webSocketHandlerRegistry) 

     webSocketHandlerRegistry.addHandler(createHandler(),
            "/handler").addInterceptors(new HttpSessionHandshakeInterceptor()
     
        @Override
        public void afterHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   @Nullable Exception ex) 

            super.afterHandshake(request, response, wsHandler, ex);

        

        @Override
        public boolean beforeHandshake(ServerHttpRequest request,
                                       ServerHttpResponse response, WebSocketHandler wsHandler,
                                       Map<String, Object> attributes) throws Exception 

            return super.beforeHandshake(request, response,
                    wsHandler, attributes)
        

    );


@Bean
public WebSocketHandler createHandler() 

    return new MyHandler(senderService);



MyHandler.java:

@Component
public class MyHandler extends TextWebSocketHandler 

WebSocketNotificationSenderService senderService;

public MyHandler(WebSocketNotificationSenderService senderService)
    this.senderService = senderService;


@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception 
    senderService.addToSession(session);


WebSocketNotificationSenderService.java:

@Service
public class WebSocketNotificationSenderService implements 
IWebSocketSenderService<WrapperWsNotification> 

private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
private ObjectMapper mapper = new ObjectMapper();

public void addToSession(WebSocketSession session) 
    sessions.add(session);


@Override
public void convertAndSend(WrapperWsNotification payload) throws JsonProcessingException 
    String payloadString = mapper.writeValueAsString(payload);
    sessions.stream().forEach(session -> 
        try 
            session.sendMessage(new TextMessage(payloadString));
         catch (IOException e) 
            e.printStackTrace();
        

    );


@Override
public void convertAndSendToUser(String user, WrapperWsNotification payload) throws 
  JsonProcessingException 
    String payloadString = mapper.writeValueAsString(payload);
    sessions.forEach(session -> 
        if (session.getPrincipal().getName().equals(user)) 
            try 
                session.sendMessage(new TextMessage(payloadString));
             catch (IOException e) 
                e.printStackTrace();
            
        
    );


我的通知发送者到 websocket;

@Component
public class NotificationConsumer 

@Autowired
WebSocketNotificationSenderService webSocket;

private Logger logger = LoggerFactory.getLogger(NotificationConsumer.class);

public void onReceiveNotification(String object) throws IOException 
    ObjectMapper objectMapper = new ObjectMapper();
    WrapperWsNotification wrapperWsNotification= objectMapper.readValue(object, WrapperWsNotification.class);
    logger.info("User where coming from redis " + wrapperWsNotification.getUser().getUsername());
    webSocket.convertAndSendToUser(wrapperWsNotification.getUser().getUsername(), wrapperWsNotification);




【问题讨论】:

【参考方案1】:

我找到了解决方案并添加了example

【讨论】:

以上是关于Spring Boot Raw WebSocket 设置主体的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot - WebSocket 握手期间出错

玩转spring boot——websocket

Spring Boot之WebSocket

Spring boot + Websocket

Spring Boot教程32——WebSocket

Spring Boot整合WebSocket实现实时消息推送