其他分享
首页 > 其他分享> > rabbitmq websocket stomp 错误payload=Access refused for user 'guest'

rabbitmq websocket stomp 错误payload=Access refused for user 'guest'

作者:互联网

https://stackoverflow.com/questions/45281024/non-loopback-access-denied-error-while-configuring-spring-websocket-with-rabbitm

 

content-length=[26]} session=_system_ text/plain payload=non-loopback access denied

 第一:

 

配置rabbitmq的配置文件: cat /etc/rabbitmq/conf.d/10-default-guest-user.conf 

追加:

loopback_users = none

另外追加用户,以及设定权限:

rabbitmqctl set_permissions dunkbird ".*" ".*" ".*"

rabbitmqctl add_user dunkbird 123456

rabbitmqctl set_user_tags dunkbird administrator

设定了tag可以进行登陆页面管理。

 

websocke 配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Value("${application.stomp-broker-relay.host:localhost}")
    private String stompBrokerRelayHost;

    @Value("${application.stomp-broker-relay.port:61613}")
    private Integer stompBrokerRelayPort;

    @Value("${application.stomp-broker-relay.login}")
    private String stompBrokerRelayLogin;

    @Value("${application.stomp-broker-relay.passcode}")
    private String stompBrokerRelayPasscode;

    /**
     * 注册 STOMP 协议端点。
     * STOMP:Simple/Streaming Text Orientated Messaging Protocol。
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 客户端连接端点
        registry.addEndpoint("/stomp").withSockJS();
    }

    /**
     * 设置 Message Broker 配置对象。
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry
            // 设置应用于所有 @MessageMapping 的前缀
            .setApplicationDestinationPrefixes("/v1")
            // 分隔符设置为句点
            .setPathMatcher(new AntPathMatcher("."));

        // 启用基于内存的 Message Broker
        //registry.enableSimpleBroker("/topic");

        // 启用基于 RabbitMQ 的 Message Broker
        registry
            .enableStompBrokerRelay("/topic")
            .setRelayHost(stompBrokerRelayHost)
            .setRelayPort(stompBrokerRelayPort)
            .setClientLogin(stompBrokerRelayLogin)
            .setClientPasscode(stompBrokerRelayPasscode)
            .setSystemLogin(stompBrokerRelayLogin)
            .setSystemPasscode(stompBrokerRelayPasscode);
    }

    /* 调试代码
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(new ChannelInterceptor() {
            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                log("I", "S", "PRE", message, channel);
                return message;
            }
            @Override
            public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
                Object destination = message.getHeaders().get("simpDestination");
                if (sent && !ObjectUtils.isEmpty(destination)) {
                    redisTemplate.convertAndSend((String) destination, new String((byte[]) message.getPayload()));
                }
                log("I", "S", "PST", message, channel);
            }
            @Override
            public boolean preReceive(MessageChannel channel) {
                log("I", "R", "PRE", null, channel);
                return true;
            }
            @Override
            public Message<?> postReceive(Message<?> message, MessageChannel channel) {
                log("I", "R", "PST", message, channel);
                return message;
            }
        });
    }

    @Override
    public void configureClientOutboundChannel(ChannelRegistration registration) {
        registration.interceptors(new ChannelInterceptor() {
            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                log("O", "S", "PRE", message, channel);
                return message;
            }
            @Override
            public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
                log("O", "S", "PST", message, channel);
            }
            @Override
            public boolean preReceive(MessageChannel channel) {
                log("O", "R", "PRE", null, channel);
                return true;
            }
            @Override
            public Message<?> postReceive(Message<?> message, MessageChannel channel) {
                log("O", "R", "PST", message, channel);
                return message;
            }
        });
    }

    private void log(String io, String when, String method, Message<?> message, MessageChannel channel) {
        System.out.printf(
            "[%s][%s][%s] TYPE: \u001B[96m%s\u001B[0m; COMMAND: \u001B[93m%s\u001B[0m; TOPIC: \u001B[92m%s\u001B[0m; MESSAGE: \u001B[94m%s\u001B[0m%n",
            io, when, method,
            message == null ? "<NULL>" : message.getHeaders().get("simpMessageType"),
            message == null ? "<NULL>" : message.getHeaders().get("stompCommand"),
            message == null ? "<NULL>" : message.getHeaders().get("simpDestination"),
            message == null ? "<NULL>" : new String((byte[]) message.getPayload())
        );
    }
    */
}

  一定注意要设定下面的配置:

.setSystemLogin(stompBrokerRelayLogin)
.setSystemPasscode(stompBrokerRelayPasscode);

否则会报下面的错误!

Received ERROR {message=[Bad CONNECT], content-type=[text/plain], version=[1.0,1.1,1.2], content-length=[32]} session=_system_ text/plain payload=Access refused for user 'guest'

标签:websocket,guest,stomp,public,Override,Message,MessageChannel,message,channel
来源: https://www.cnblogs.com/dunkbird/p/15165501.html