连接到Websocket期间的身份验证

时间:2019-07-22 13:06:58

标签: java spring spring-security websocket

我正在尝试使用SockJSAngularSpring学习网络套接字。消息的连接,发送和接收就像一个超级按钮。

我的问题是,使用Spring Security。我无法在连接阶段进行身份验证,因此每次发送消息时,我都会验证并设置我的身份验证用户。

我将令牌作为URL的一部分发送(我认为websocket不支持标头):

// Other code

@MessageMapping("/send/message/{juegoDatos}/{token}")
public void onRecivedMessage(@DestinationVariable String juegoDatos, @DestinationVariable String token, @RequestBody String message) {

   authService.setAuthUser(token);
   template.convertAndSend("/chat", DateFormatterUtil.formatSimpleDate(new Date()) + " " + message);
}

这是我的配置:

public class WebsocketConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer {


   @Override
   public void registerStompEndpoints(StompEndpointRegistry registry) {
      registry.addEndpoint("/socket")
              .setAllowedOrigins("*")
              .withSockJS();
   }

   @Override
   protected boolean sameOriginDisabled() {
      return true;
   }

   @Override
   public void configureMessageBroker(MessageBrokerRegistry registry) {
      registry.setApplicationDestinationPrefixes("/app")
              .enableSimpleBroker("/chat");
   }

有什么方法可以覆盖我的连接端点?

localhost:8080/socket

我在想,也许我可以将令牌作为URL发送给它,并且只进行一次身份验证。 (实际上,如果我可以覆盖连接端点,甚至可以建立聊天室和其他东西)

谢谢!

0 个答案:

没有答案