为什么我可以通过MQTT连接到Google Cloud Iot Core中的设备?

发布于 2025-01-17 07:13:31 字数 1901 浏览 4 评论 0原文

我想将 Spring Boot 上的后端连接到 Google Cloud Iot-Core 中硬件设置中的设备之一。我基本上复制了 此 github 存储库 并调整了身份验证和连接选项。当我运行程序时,它抛出此异常:

Caused by: org.springframework.beans.BeanInstantiationException: 无法实例化 [org.eclipse.paho.client.mqttv3.MqttClient]: Factory 方法 'connectToMqttClient' 抛出异常;嵌套异常是不正确的用户名或密码 (4)

要创建密码,我使用此方法,

private static String createJwtRsa(String projectId, String privateKeyFile)
        throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    DateTime now = new DateTime();

    JwtBuilder jwtBuilder =
            Jwts.builder()
                    .setIssuedAt(now.toDate())
                    .setExpiration(now.plusMinutes(20).toDate())
                    .setAudience(projectId);

    byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile));
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");

    return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact();
}

但我遇到了 privateKeyFile 格式的问题。在 IoT-core 上,设备具有 RS256_X509 的公钥

在此处输入图像描述

但 Spring Boot 只允许 RS256_PKCS8 密钥格式,所以我必须这样做:

  1. 我生成了格式为 X509 openssl req -x509 -nodes -newkey rsa:2048 -keyout 的密钥rsa_private.pem -out rsa_cert.pem -subj "/CN=unused" 它还生成了证书 rsa_cer.pem
  2. 我将密钥转换为 PKCS8 格式 openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private .pem -out private_key.der -nocrypt
  3. 我在Iot-core中添加为公钥的证书和我在 createJwtRsa 方法中使用了 rsa_private.der

从那以后我得到了异常:用户名和密码不正确。我能做什么来修复它?

编辑:

我还尝试生成正常的 RSA256 密钥并将其转换为 PKCS8 格式,并且我得到了相同的异常。与ES256密钥相同

I want to connect my backend on Spring boot to one of the devices in my hardware setup in Google Cloud Iot-Core. I basically copied this github repository and adjusted the authentication and connection options. When I run my program it throws this exception:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.eclipse.paho.client.mqttv3.MqttClient]: Factory method 'connectToMqttClient' threw exception; nested exception is Incorrect username or password (4)

To create password I use this method

private static String createJwtRsa(String projectId, String privateKeyFile)
        throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    DateTime now = new DateTime();

    JwtBuilder jwtBuilder =
            Jwts.builder()
                    .setIssuedAt(now.toDate())
                    .setExpiration(now.plusMinutes(20).toDate())
                    .setAudience(projectId);

    byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile));
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");

    return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact();
}

I had an issue with privateKeyFile format. On Iot-core the device has public key of RS256_X509

enter image description here

But Spring Boot only allows RS256_PKCS8 key formats, so I had to do this:

  1. I generated key of format X509 openssl req -x509 -nodes -newkey rsa:2048 -keyout rsa_private.pem -out rsa_cert.pem -subj "/CN=unused" it also generated certificate rsa_cer.pem
  2. I converted the key to PKCS8 format openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private.pem -out private_key.der -nocrypt
  3. The certificate I added as public key in Iot-core and rsa_private.der I used in my createJwtRsa method

Since then I get the exception: Incorrect Username and password. What can I do to fix it?

Edit:

I also tried generating normal RSA256 key and convert it to PKCS8 format, and I get the same exception. The same with ES256 key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜深人未静 2025-01-24 07:13:31

我设法解决了这个问题! clientId 不正确

final String mqttClientId = String.format("projects/%s/locations/%s/registries/%s/devices/%s",
options.projectId, options.cloudRegion, options.registryId, options.gatewayId);

我的 gatewayId 是空字符串,因为我们没有在 IoT Core 上创建网关,我们直接连接到设备。
所以今天我将gatewayId设置为deviceId,后端成功连接到设备。
所以现在这条线看起来像这样

final String mqttClientId = String.format("projects/%s/locations/%s/registries/%s/devices/%s",options.projectId, 
 options.cloudRegion, options.registryId, options.deviceId);

I managed to fix the issue! the clientId was incorrect

final String mqttClientId = String.format("projects/%s/locations/%s/registries/%s/devices/%s",
options.projectId, options.cloudRegion, options.registryId, options.gatewayId);

My gatewayId was an empty string because we don't have a gateway created on Iot Core, we connect directly to devices.
So today I set gatewayId to deviceId and the backend connected successfully to the device.
So now the line looks like this

final String mqttClientId = String.format("projects/%s/locations/%s/registries/%s/devices/%s",options.projectId, 
 options.cloudRegion, options.registryId, options.deviceId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文