账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springboot2结合websocket时为何ChannelInterceptorAdapter无法注入bean?
    15
    0
    package com.qky.qingchi.config;
    
    import com.qky.qingchi.config.filter.UserMessageInterceptor;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.messaging.simp.config.ChannelRegistration;
    import org.springframework.messaging.simp.config.MessageBrokerRegistry;
    import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
    import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
    import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
    
    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
        @Override
        public void configureMessageBroker(MessageBrokerRegistry registry) {
            registry.enableSimpleBroker("/queue", "/topic");
        }
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/webSocketServer").setAllowedOrigins("*").withSockJS();
        }
    
        /**
         * 配置客户端入站通道拦截器
         */
        @Override
        public void configureClientInboundChannel(ChannelRegistration registration) {
            registration.interceptors(new UserMessageInterceptor());
        }
    }
    
    package com.qky.qingchi.config.filter;
    
    import com.qky.qingchi.config.websocket.WebSocketUser;
    import com.qky.qingchi.util.TokenUtils;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
    import org.springframework.messaging.Message;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.simp.stomp.StompCommand;
    import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
    import org.springframework.messaging.support.ChannelInterceptorAdapter;
    import org.springframework.messaging.support.MessageHeaderAccessor;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component
    public class UserMessageInterceptor extends ChannelInterceptorAdapter {
    
        @Resource
        private RedisMessageListenerContainer redisContainer;
    
        @Resource
        private RedisConnectionFactory connectionFactory;
    
        @Resource
        private MessageListenerAdapter messageListener;
    
        @Resource
        private ChannelTopic topic;
    
        @Resource
        private String qkyname;
    
        /**
         * 获取包含在stomp中的用户信息
         */
        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            StompHeaderAccessor accessor =
                    MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
            System.out.println(accessor.getLogin());
            System.out.println(accessor.getCommand());
            System.out.println("-----------------------");
            System.out.println(redisContainer);
            System.out.println(messageListener);
            System.out.println(topic);
            System.out.println(qkyname);
            System.out.println("-----------------------");
            System.out.println("触发websocket连接");
            //1. 判断是否首次连接请求
            if (StompCommand.CONNECT.equals(accessor.getCommand())) {
                //2. 验证是否登录
                String token = accessor.getNativeHeader("token").get(0);
                System.out.println(token);
                if (TokenUtils.isCorrect(token)) {
                    String phoneNumber = TokenUtils.getPhoneNumByToken(token);
                    accessor.setUser(new WebSocketUser(phoneNumber));
                /*    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
                    container.setConnectionFactory(connectionFactory);
                    container.addMessageListener(messageListener, new ChannelTopic(phoneNumber));*/
                    return message;
                } else {
                    return null;
                }
            }
            //不是首次连接,已经成功登陆
            return message;
        }
    }
    package com.qky.qingchi.config.redis;
    
    import com.qky.qingchi.util.UserUtils;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.MessageListener;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
    import org.springframework.messaging.simp.SimpMessagingTemplate;
    
    import javax.annotation.Resource;
    
    @Configuration
    public class RedisSubListenerConfig {
    
        @Resource
        private SimpMessagingTemplate messagingTemplate;
    
        @Bean
        MessageListenerAdapter messageListener() {
            //abstract methods overwrite
            return new MessageListenerAdapter((MessageListener) (message, pattern) -> {
                System.out.println("Message received: " + message.toString());
                //message中带user信息,然后根据user推送message
    //            replyUser.getUsername()
                messagingTemplate.convertAndSendToUser(UserUtils.getUser().getPhoneNum(), "/queue/notifications", message);
            });
        }
    
        @Bean
        RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
            RedisMessageListenerContainer container
                    = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            container.addMessageListener(messageListener(), topic());
            return container;
        }
    
        @Bean
        ChannelTopic topic() {
            return new ChannelTopic("messageQueue");
        }
    
        @Bean("qkyname")
        public String qkyname() {
            System.out.println("注册秦开远名字bean");
            return "qinkaiyuan";
        }
    }

    注入的内容在其他类都是可以获得的,就在这个类里面无法获取,这是为什么啊

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版

    回答动态

    师傅被妖怪抓走了:发布了悬赏问题火爆全网的“元宇宙”到底是什么?预计能赚取 1积分收益
    酒后略带风情:发布了悬赏问题Android内存中Graphics与Code占用过高预计能赚取 10积分收益
    扛起拖把扫天下:发布了悬赏问题typescript如何导出接口配置?求解,谢谢!预计能赚取 10积分收益
    发布了悬赏问题Vue 如何改变返回页面的路径?如让B页面只能返回到A页面.预计能赚取 11积分收益
    发布了悬赏问题vue H5移动端;底部bottom固定定位,安卓弹起键盘导致页面变形如何解决?预计能赚取 10积分收益
    发布了悬赏问题请问为什么tomcat8.5 GET请求中文正常,POST请求还需要设置UTF-8.预计能赚取 10积分收益
    发布了悬赏问题vue2.5+ts创建以及引入jq的方法预计能赚取 11积分收益
    情场浪女:发布了悬赏问题import ggplot时报错module 'pandas' has no attribute 'tslib'预计能赚取 10积分收益
    见心书画许老师18753375161:发布了悬赏问题echarts3 legend禁止点击事件,允许悬浮事件预计能赚取 11积分收益
    aa_92cb0bd09f:发布了悬赏问题codewar上的一个问题预计能赚取 11积分收益