Eclipse Kapua经纪人:未授权订阅主题

时间:2017-07-13 16:16:00

标签: mqtt paho kapua

我正在尝试从Eclipse Paho MQTT客户端订阅一个简单的主题“foo”。

代理由Eclipse Kapua管理,可通过tcp:// localhost:1883访问,凭据“kapua-broker”和“kapua-password”。

我以这种方式发布价值:

send(new Payload.Builder().put("testKey","testVal"),"foo");

这基本上发送了一个主题为“foo”的地图(“testKey”,“testVal”)。要订阅此主题,我有以下代码(host =“localhost”,port = 1883):

    String topic = "foo";
    String broker = "tcp://"+host+":"+Integer.toString(port);
    String clientId = "supply-chain-control-simulation-listener";
    String username = "kapua-broker";
    String password = "kapua-password";

    try {
        MqttClient client = new MqttClient(broker, clientId);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        connOpts.setCleanSession(true);
        logger.info("Connecting to broker: "+broker);
        client.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable throwable) {
                logger.info("Subscriptions stopped");
            }

            @Override
            public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                logger.info(s);
                logger.info(mqttMessage.getPayload().toString());
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });
        client.connect(connOpts);
        if (client.isConnected())
            logger.info("Connected");
        else
            logger.error(client.getDebug().toString());
        client.subscribe(topic,2);
    } catch(MqttException me) {
        logger.error("reason "+me.getReasonCode());
        logger.error("msg "+me.getMessage());
        logger.error("loc "+me.getLocalizedMessage());
        logger.error("cause "+me.getCause());
        logger.error("excep "+me);
        me.printStackTrace();
    }

连接有效,但订阅输出此错误:

15:40:03.240 [ActiveMQ NIO Worker 0]警告oekbcpKapuaSecurityBrokerFilter - 用户1:kapua-broker(supply-chain-control-simulation-listener - tcp://172.17.0.1:40888 - conn id 1734706196170193882)无权阅读:topic:// VirtualTopic.foo

2 个答案:

答案 0 :(得分:1)

在致电subscribe后不要立即connect,而是将该来电转移到connectComplete回调中:

IMqttAsyncClient client = new MqttAsyncClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setUserName(username);
connOpts.setPassword(password.toCharArray());
connOpts.setCleanSession(true);
logger.info("Connecting to broker: "+broker);
client.setCallback(new MqttCallbackExtended() {
    @Override
    public void connectComplete(boolean reconnect, String brokerAddress) {
        logger.info("Connected");
        client.subscribe(topic,2);
    }
    @Override
    public void connectionLost(Throwable throwable) {
        logger.info("Subscriptions stopped");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        logger.info(s);
        logger.info(mqttMessage.getPayload().toString());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
});
client.connect(connOpts);

也就是说,您的错误可能来自您正在使用的MQTT代理,您需要对其进行配置以允许访问该主题。

答案 1 :(得分:1)

在Kapua,您可以根据您的用户许可发布/订阅。

如果您的用户仅拥有broker:connect权限,则您只能发布/订阅主题:

{account-name}/{connectionClientId}/{semanticTopic}

在您的特定情况下,您应该发布/订阅主题:

kapus-sys/supply-chain-control-simulation-listener/foo

kapua-sys是用户kapua-broker所属的帐户名,  而supply-chain-control-simulation-listener是用于创建连接的clientId。

请注意,用于连接的用户名和帐户名是Kapua中的两个不同内容。一个帐户有多个用户。