为什么我可以通过MQTT连接到Google Cloud Iot Core中的设备?
我想将 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 密钥格式,所以我必须这样做:
- 我生成了格式为 X509 openssl req -x509 -nodes -newkey rsa:2048 -keyout 的密钥rsa_private.pem -out rsa_cert.pem -subj "/CN=unused" 它还生成了证书 rsa_cer.pem
- 我将密钥转换为 PKCS8 格式 openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private .pem -out private_key.der -nocrypt
- 我在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
But Spring Boot only allows RS256_PKCS8 key formats, so I had to do this:
- 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
- I converted the key to PKCS8 format openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private.pem -out private_key.der -nocrypt
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法解决了这个问题! clientId 不正确
我的 gatewayId 是空字符串,因为我们没有在 IoT Core 上创建网关,我们直接连接到设备。
所以今天我将gatewayId设置为deviceId,后端成功连接到设备。
所以现在这条线看起来像这样
I managed to fix the issue! the clientId was incorrect
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