AWS IoT Core 自定义授权方

时间:2021-02-22 15:43:04

标签: amazon-web-services aws-iot-core

我正在尝试使用 AWS IoT Core 自定义授权程序,如下所示 (https://docs.aws.amazon.com/iot/latest/developerguide/config-custom-auth.html)。我开发了 lambda 并能够使用 HTTP 端点 (https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_Publish.html) 进行发布,也能够通过运行 aws iot test-invoke-authorizer --authorizer-name <name> --mqtt-context "username=***,password=***,clientId=***" 由 AWS CLI 调用它。但是,当我尝试使用任何其他客户端时都不起作用(我首先使用了 Moss、MQTT Explorer 和 paho 客户端)。有了这些,我只能在客户端超时,而在服务器端什么也没有。我还尝试了 Java AWS IoT Core SDK,连接时在 boolean sessionPresent = connected.get(); 中出现 TLS 协商失败。作为端点,我使用 IoT:Data-ATS。文档 (https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html) 中有一条令人困惑的行,它在表中显示应在端口 443 中使用带有 MQTT 的自定义身份验证,并带有脚注。脚注说端口 443 中的自定义身份验证不起作用。这没有意义。

有什么想法或帮助吗?

Mosquitto_sub 被截断: mosquitto_sub -d -h **** -p 443 -u username?x-amz-customauthorizer-name=*** -P test -t test --cafile /etc/ssl/certs/Amazon_Root_CA_1.pem -i ***

Paho 剪断了:

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.nio.charset.StandardCharsets;

public class Paho implements MqttCallback {
    public static void execute(){
        try {
            MqttClient client = new MqttClient("ssl://***:443","***",new MemoryPersistence());

            client.connect();
            client.setCallback(new Paho());
            while (true){
                client.publish("test","test".getBytes(StandardCharsets.UTF_8),0, false);
                Thread.sleep(500);
            }
        } catch (MqttException | InterruptedException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        System.out.println(new String(mqttMessage.getPayload()));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }

AWS 客户端截图:

public class AWS implements MqttClientConnectionEvents  {
    public static void execute(){
        try {
           

        try(EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
            HostResolver resolver = new HostResolver(eventLoopGroup);
            ClientBootstrap clientBootstrap = new ClientBootstrap(eventLoopGroup, resolver);
            AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newDefaultBuilder()) {



            builder.withBootstrap(clientBootstrap)
                    .withConnectionEventCallbacks(new AWS())
                    .withClientId("****")
                    .withEndpoint("***")
                    .withCleanSession(true);


            try(MqttClientConnection connection = builder.build()) {

                CompletableFuture<Boolean> connected = connection.connect();
                try {
                    boolean sessionPresent = connected.get();
                    System.out.println("Connected to " + (!sessionPresent ? "new" : "existing") + " session!");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw new RuntimeException("Exception occurred during connect", ex);
                }

                CountDownLatch countDownLatch = new CountDownLatch(10);

                CompletableFuture<Integer> subscribed = connection.subscribe("test", QualityOfService.AT_LEAST_ONCE, (message) -> {
                    String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
                    System.out.println("MESSAGE: " + payload);
                    countDownLatch.countDown();
                });

                subscribed.get();

                int count = 0;
                while (count++ < 10) {
                    CompletableFuture<Integer> published = connection.publish(new MqttMessage("test", "test".getBytes(), QualityOfService.AT_LEAST_ONCE, false));
                    published.get();
                    Thread.sleep(1000);
                }

                countDownLatch.await();

                CompletableFuture<Void> disconnected = connection.disconnect();
                disconnected.get();
            }
        } catch (Exception  ex) {
            System.out.println("Exception encountered: " + ex.toString());
        }

        System.out.println("Complete!");
    }

    @Override
    public void onConnectionInterrupted(int errorCode) {

    }

    @Override
    public void onConnectionResumed(boolean sessionPresent) {

    }
}

0 个答案:

没有答案