Spring 4 WebSocket发送到指定用户:convertAndSendToUser函数

有时推送需要推送到指定的用户,这时就需要使用@SengToUser注解或者convertAndSendToUser这个函数,需要注意的是,推送到特定用户不一定非要使用Spring Security这个框架来实现登录和访问控制,我在项目中使用的是Apache Shiro。
在用户登录之后,在Session里面设置相应的Principal:

        UsernamePasswordToken token = new UsernamePasswordToken(phonenum, password);
        token.setRememberMe(true);
        currentUser.login(token);
        Session session = currentUser.getSession();
        session.setAttribute("user_id", user_id);
        session.setAttribute("phonenum", phonenum);

然后就可以发送给指定用户了:

    public void handleMessage(Baobiaoorder order) {
        log.info("收到来自RabbitMQ的消息:" + order.getOuttradeno());
//        operations.convertAndSend("/topic/pay-result", order);
        operations.convertAndSendToUser(order.getPhonenum(), "/queue/pay-result", order);
    }

其中order.getPhonenum()便是Session里面的手机号,因为我是把手机号作为Shiro的Principal。此时消息会发送给/queue/pay-result这个目的地,但是客户端需要订阅的是/user/queue/pay-result。值得注意的是,按照惯例,最好把这种发送给特定用户的、非广播式的目的地设置为queue而不是topic。这样就能做到发送消息给指定的用户了。

要监听Token过期并通过Websocket发送指定用户,您可以使用Spring Security提供的事件机制,结合Spring Cloud Gateway和Spring WebSocket实现。 具体实现步骤如下: 1. 在Spring Cloud Gateway中配置Token过期事件监听器。可以通过实现`org.springframework.context.ApplicationListener`接口来监听`org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent`事件,代码如下: ```java @Component public class TokenExpiredEventListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> { @Autowired private SimpMessagingTemplate messagingTemplate; @Override public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) { String username = event.getAuthentication().getName(); messagingTemplate.convertAndSendToUser(username, "/queue/token-expired", "Token expired"); } } ``` 在上述代码中,我们实现了一个`TokenExpiredEventListener`类,用于监听Token过期事件。当事件触发时,我们使用`SimpMessagingTemplate`将消息发送指定用户。在这里,我们使用了`/queue/token-expired`主题,您可以根据您的实际情况进行修改。 2. 在Spring WebSocket中添加消息处理器。可以通过实现`org.springframework.messaging.handler.annotation.MessageMapping`注解来处理消息,代码如下: ```java @Controller public class TokenExpiredMessageHandler { @MessageMapping("/queue/token-expired") public void handleTokenExpiredMessage() { // do something } } ``` 在上述代码中,我们实现了一个`TokenExpiredMessageHandler`类,用于处理Token过期消息。在这里,我们使用了`/queue/token-expired`主题,与上述事件监听器中的主题保持一致。 3. 在Spring Cloud Gateway中添加WebSocket支持。可以通过`org.springframework.cloud.gateway.filter.factory.WebsocketGatewayFilterFactory`过滤器工厂来实现,代码如下: ```yaml spring: cloud: gateway: routes: - id: notification uri: ws://localhost:8080 predicates: - Path=/notification/** filters: - name: WebSocket args: factory: org.springframework.cloud.gateway.filter.factory.WebsocketGatewayFilterFactory ``` 在上述代码中,我们配置了一个路由(route),将请求转发给ws://localhost:8080地址。我们还使用了WebSocket过滤器,以便在请求中启用WebSocket支持。 希望这些代码对您有所帮助。如果您有任何疑问,请随时问我。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值