【emq jwt接入认证问题】RS256算法无法认证

环境信息

  • EMQ X 版本:v4.3.5
  • 操作系统及版本1:Windows10, docker部署,不方便看日志后又使用如下centos系统
  • 操作系统及版本2:CentOS Linux release 7.9.2009 (Core)
  • 备注:在以上两个操作系统下操作,问题现象一样

mqtt客户端测试工具

  • MQTTX v1.6.0

JWT生成软件环境信息

  • JWT-token生成服务器:java11,spring boot
  • 使用Java库:io.jsonwebtoken.Jwts
  • JWT生成算法:SignatureAlgorithm.RS256
    Java生成JWT token的代码在下方截图所示

问题描述

测试流程如下:

  1. 前端登录我们的web服务器【Java spring boot】,web服务器生成jwt token【RS256算法】给前端。
  2. 在MQTTX 工具中新建一个mqtt连接,随意填上client id,username
  3. 我手动从页面拷贝token到MQTTX 工具中的password输入框,准备使用这个token来连接emqx mqtt服务器
  4. 此时 Connect出现错误,有截图
  5. 为了排除环境部署等低级错误,使用HS256 JWT加密算法做对比测试,mqtt连接正常,HS256 token字符串的生成是在emqx的dashboard->Plugins->emqx_auth_jwt右侧manage中生成的
    备注:dashboard中emqx_auth_jwt插件的管理界面无法生成RS256的token

配置文件及日志

emqx_auth_jwt.conf内容如下:

##--------------------------------------------------------------------
## JWT Auth Plugin
##--------------------------------------------------------------------

## HMAC Hash Secret.
##
## Value: String
#auth.jwt.secret = emqxsecret

## RSA or ECDSA public key file.
##
## Value: File
auth.jwt.pubkey = /etc/emqx/certs/rsa.pub

## The JWKs server address
##
## see: http://self-issued.info/docs/draft-ietf-jose-json-web-key.html
##
#auth.jwt.jwks = https://127.0.0.1:8080/jwks

## The JWKs refresh interval
##
## Value: Duration
#auth.jwt.jwks.refresh_interval = 5m

## From where the JWT string can be got
##
## Value: username | password
## Default: password
auth.jwt.from = password

## Enable to verify claims fields
##
## Value: on | off
auth.jwt.verify_claims = off

## The checklist of claims to validate
##
## Configuration format: auth.jwt.verify_claims.$name = $expected
##   - $name: the name of the field in the JWT payload to be verified
##   - $expected: the expected value
##
## The available placeholders for $expected:
##   - %u: username
##   - %c: clientid
##
## For example, to verify that the username in the JWT payload is the same
## as the client (MQTT protocol) username
#auth.jwt.verify_claims.username = %u
1 个赞

PS1: 页面上当前只能生产基于 Secert 的 JWT Token. 基于非对称的(例如 RS256,ES256 等)还不支持的。

关于连接失败的话,你这边方便发下一个测试用的 rsa.pub 和 你用 java 生成的 token 么? 我们这边复现下试试。

(文件可以 email 到 heeejianbo@emqx.io)

已经将文件发送至邮件

Hi @JonyRyan 看起来是你的 rsa.pub 文件的格式是不正确的。

emqx-auth-jwt 插件要求的是一个 pem 格式的 公钥文件。类似于:

-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

这种格式

1 个赞

Hi,@heeejianbo

  • rsa的格式以前还真没注意过,之前一直用的java库,在web服务器初次启动时自检然后创建的公钥和私钥。今晚经过查阅资料,了解到这个生成的是pkcs1格式的公钥和私钥。这显然不是emqx_auth_jwt需要的pem格式。

  • 然后对web服务器token认证程序做了改造,使用openssl生成pem格式的公钥和私钥,将web服务器改造为使用pem格式的私钥生成jwt-token。
    最后MQTTX上测试连接emqx,成功!

    非常感谢你们的支持! :smiley:

2 个赞