Art*_*r C 13 oauth jwt mqtt json-web-token auth0
按照本Auth0文章中的说明,我使用"JWT"作为用户名并使用JWT令牌作为密码成功验证了MQTT客户端.
然而,在我的用例中,JWT令牌是短暂的.客户端必须在当前令牌的到期日期之前获取新令牌,然后将其提供给MQTT服务器.否则,服务器终止连接.
我的问题是:如何实现令牌更新?它是来自客户的发布消息吗?哪个主题?我是否断开客户端连接,并让客户端使用新令牌重新进行身份验证?或者还有另一种方式吗?
小智 1
考虑刷新 JWT 令牌很重要,因为令牌有过期日期。如果设备通过 MQTT 连接且其令牌过期,MQTT 代理应自动断开设备与代理的连接。您可以通过自动刷新其令牌来防止设备断开连接。
以下示例说明如何检查令牌是否已过期,如果过期,如何在不断开设备连接的情况下使用新令牌重新连接。
long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000;
if (secsSinceRefresh > (options.tokenExpMins * 60)) {
System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh);
iat = new DateTime();
if (options.algorithm.equals("RS256")) {
connectOptions.setPassword(
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
} else if (options.algorithm.equals("ES256")) {
connectOptions.setPassword(
createJwtEs(options.projectId, options.privateKeyFile).toCharArray());
} else {
throw new IllegalArgumentException(
"Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'.");
}
client.disconnect();
client.connect();
attachCallback(client, options.deviceId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
481 次 |
| 最近记录: |