java - Spring websocket 与rabbitmq - 在订阅级别添加安全性

标签 java spring spring-boot websocket spring-websocket

在我的spring-boot我有的应用程序spring-securityspring-websocket 。下面是我的 websocket 配置。

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport
        implements WebSocketMessageBrokerConfigurer {

    @Value( "${rabbitmq.host}" )
    private String rabbitmqHost;
    @Value( "${rabbitmq.stomp.port}" )
    private int rabbitmqStompPort;
    @Value( "${rabbitmq.username}" )
    private String rabbitmqUserName;
    @Value( "${rabbitmq.password}" )
    private String rabbitmqPassword;

    @Override
    public void configureMessageBroker( MessageBrokerRegistry registry )
    {
        registry.enableStompBrokerRelay("/topic", "/queue").setRelayHost(rabbitmqHost).setRelayPort(rabbitmqStompPort)
                .setSystemLogin(rabbitmqUserName).setSystemPasscode(rabbitmqPassword);
        registry.setApplicationDestinationPrefixes("/app");
    }

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

并且,

public class CustomSubProtocolWebSocketHandler extends SubProtocolWebSocketHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomSubProtocolWebSocketHandler.class);

    @Autowired
    private UserCommons userCommons;

    CustomSubProtocolWebSocketHandler(MessageChannel clientInboundChannel,
                                      SubscribableChannel clientOutboundChannel) {
        super(clientInboundChannel, clientOutboundChannel);
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        LOGGER.info("************************************************************************************************************************New webSocket connection was established: {}", session);
        String token = session.getUri().getQuery().replace("token=", "");
        try
        {
            String user = Jwts.parser().setSigningKey(TokenConstant.SECRET)
                    .parseClaimsJws(token.replace(TokenConstant.TOKEN_PREFIX, "")).getBody().getSubject();
            Optional<UserModel> userModelOptional = userCommons.getUserByEmail(user);
            if( !userModelOptional.isPresent() )
            {
                LOGGER.error(
                        "************************************************************************************************************************Invalid token is passed with web socket request");
                throw new DataException(GeneralConstants.EXCEPTION, "Invalid user", HttpStatus.BAD_REQUEST);
            }
        }
        catch( Exception e )
        {
            LOGGER.error(GeneralConstants.ERROR, e);
        }
        super.afterConnectionEstablished(session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        LOGGER.error("************************************************************************************************************************webSocket connection was closed");
        LOGGER.error("Reason for closure {} Session: {} ", closeStatus.getReason(),session.getId() );
        super.afterConnectionClosed(session, closeStatus);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

        LOGGER.error("************************************************************************************************************************Connection closed unexpectedly");
        LOGGER.error(GeneralConstants.ERROR, exception);
        super.handleTransportError(session, exception);
    }
}

为了在建立连接时添加安全层,我接受连接 URL 中的 token 。因此客户端应用程序将连接到 /ws?token=***** .

但是为了将消息发送给特定用户,我使用 user_id 构建订阅 URL。例如,如果登录用户id为23,则客户端将订阅/topic/noti.23从服务器端我将消息发送到 /topic/noti.23 .

 public void sendMessagesToTheDestination( WebSocketNotificationResponseBean webSocketNotificationResponseBean,
                List<String> paths )
        {
            try
            {
                for( String path : paths )
                {
                    LOGGER.info("Sending message to path: {}", path);
                    messagingTemplate.convertAndSend(path, webSocketNotificationResponseBean);
                    LOGGER.info("Sent message to path: {}", path);
                }
            }
            catch( Exception e )
            {
                LOGGER.error("Error while sending web socket notification {}", e);
            }
        }
}

哪里path/topic/noti.<user_id> .

上述实现正在运行。

现在的问题是,任何拥有有效 token 的用户都可以连接到 websocket,然后从浏览器控制台可以手动订阅任何 URL。例如,user_id 23的用户可以进入浏览器控制台添加sockjs CDN并可以订阅/topic/noti.56并开始接收user_id为56的用户的消息。

如何在这里添加安全层?

我尝试使用convertAndSendToUser但不理解 session 部分,即服务器如何理解 session 以及我应该如何从客户端订阅。

谢谢

最佳答案

您不需要动态构建目的地。您可以订阅“/user/queue/wishes”等目的地,并且仍然可以发送私有(private)消息。

String queueName = "/user/" + username  + "/queue/wishes";
simpMessagingTemplate.convertAndSend(queueName, message);

关于java - Spring websocket 与rabbitmq - 在订阅级别添加安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62267173/

相关文章:

java - 为入站网关回复超时设置 HTTP 状态代码?

使用 Hamcrest 进行 Spring MVC 测试 : how count and test the properties number/size of an object inside a Model

spring-boot 从 1.3.2 升级到 1.3.3 : logback issue

java - 当我尝试在 Spring Boot 和 MVC 中使用 get 映射时出现异常

eclipse - Eclipse 中的离线 Java API 文档?

java - Scala 中的 Drools Expert 输出对象

java - 使用太多的听众是否有潜在的减速?

java - TypedQuery 给出 NullPointerException

spring - JdbcTemplate - 替换问号

java - JAVA 对象创建之前不执行构造函数代码