前言
起步
| 1. 内置数据源 |
| username认证 |
| clientid认证 |
| 2. 外部数据库 |
| mysql |
| redis |
| 3. 其他 |
| jwt |
| http |
| |
| |
| docker exec -it 79cc8ddd4941 /bin/sh |
| |
| cd /opt/emqx/etc |
| |
| vi emqx.conf |
| |
| /allow_anonymous |
| |
| |
| |
| emqx restart |
| |

| |
| cd /opt/emqx/etc/plugins |
| |
| vi emqx_auth_mysql.conf |
| |


| 输入用户名和密码,emqx根据不同插件连接不同的数据源,从数据源中查询用户名和密码进行认证 |

| 当有多个认证方式时,先使用username进行认证,认证通过则连接成功,认证失败则不能连接,当username忽略认证时,则采用clientid进行认证,若client也是忽略,那就再下一个认证方式 |

username认证
使用username认证,并添加认证数据
- 启动如下插件

| |
| cat emqx_auth_username.conf |
| |
| |
| |

| @hostname = 124.224.115.158 |
| @port=8081 |
| @contentType=application/json |
| @userName=admin |
| @password=public |
| |
| |
| GET http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| |
| POST http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| { |
| "username": "user1", |
| "password": "123456" |
| } |
| |
| |
| |
| PUT http://{{hostname}}:{{port}}/api/v4/auth_username/user HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| { |
| "password": "user" |
| } |
| |
| |
| |
| GET http://{{hostname}}:{{port}}/api/v4/auth_username/user HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| |
| |
| DELETE http://{{hostname}}:{{port}}/api/v4/auth_username/goudan1 HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
- 下载安装mqttx客户端
https://github.com/emqx/MQTTX

- 连接服务端,2个连接都可以使用同一账号

- test1发送

-test2订阅

clientid认证
- 启动clientid的插件

- 查看配置文件
| 同样已经默认设置好了密码加密方式 |
| 也可以在这里配置client + password,但不推荐 |
| 推荐使用emqx提供的api添加client和password |

| |
| POST http://{{hostname}}:{{port}}/api/v4/auth_clientid HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| { |
| "clientid": "emq-client", |
| "password": "123456" |
| } |
| |
| |
| GET http://{{hostname}}:{{port}}/api/v4/auth_clientid HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| |
| |
| PUT http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client1 HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| { |
| "password": "654321" |
| } |
| |
| |
| |
| GET http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client1 HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| |
| |
| DELETE http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client2 HTTP/1.1 |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
- 使用mqttx客户端工具测试
- 先断开username的连接

- 使用clientid连接
| 注意这里的client id需要填自己刚才通过api添加的数据 |
| host填写服务端ip地址 |
| username也需要填写,但不要填写正确的,否则就会就username的认证了 |
| password必须填写clientid对应的密码 |


- 收发消息测试


http认证
| client <=> EMQ X <=> 自建http认证应用(自己的认证数据源) |
| |
| 客户端将用户名和密码发送到emqx服务端,服务端将数据发送到自建的http认证应用 |
| 在自建的http认证应用中认证成功,将返回结果返回给emqx服务端 |
| 服务端再决定客户端能否连接emqx |
| 如下部分表示,emqx服务端在请求自建http认证应用时是使用http还是https的方式,默认是http的方式 |

| # 表示emqx服务端在请求自建http认证应用时可以接收的请求头编码、可以接收哪些请求头,默认是所有格式 |
| # |
| # |
| # |
| # |
| # |
| # |
| |
| # 表示emqx服务端在请求自建http认证应用时,自建http认证应用的ip地址,请求方式默认是post,以及可以接收哪些参数 |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| # |
| auth.http.auth_req = http://127.0.0.1:8991/mqtt/auth |
| # |
| auth.http.auth_req.method = post |
| # |
| auth.http.auth_req.params = clientid=%c,username=%u,password=%P |
| |
| # |
| auth.http.request.retry_times = 3 |
| auth.http.request.retry_interval = 1s |
| auth.http.request.retry_backoff = 2.0 |
| |
| 打开配置文件,配置为自己的http认证应用的ip |
| auth.http.auth_req = http://192.168.200.10:8991/mqtt/auth |
| |
| # 新建一个spring boot工程 |
| # 导入依赖 |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| <scope>runtime</scope> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-configuration-processor</artifactId> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| <scope>test</scope> |
| <exclusions> |
| <exclusion> |
| <groupId>org.junit.vintage</groupId> |
| <artifactId>junit-vintage-engine</artifactId> |
| </exclusion> |
| </exclusions> |
| </dependency> |
| |
| # 编写配置文件 |
| server: |
| port: 8991 |
| spring: |
| application: |
| name: emq-demo |
| |
| # 编写控制器 |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.springframework.http.HttpStatus; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.util.StringUtils; |
| import org.springframework.web.bind.annotation.PostMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RequestParam; |
| import org.springframework.web.bind.annotation.RestController; |
| import javax.annotation.PostConstruct; |
| import java.util.HashMap; |
| |
| @RestController |
| @RequestMapping("/mqtt") |
| public class AuthController { |
| |
| private static final Logger log = LoggerFactory.getLogger(AuthController.class); |
| |
| private HashMap<String,String> users; |
| |
| @PostConstruct |
| public void init(){ |
| users = new HashMap<>(); |
| users.put("user","123456"); |
| users.put("emq-client2","123456"); |
| users.put("emq-client3","123456"); |
| } |
| |
| @PostMapping("/auth") |
| public ResponseEntity auth(@RequestParam("clientid") String clientid, |
| @RequestParam("username") String username, |
| @RequestParam("password") String password){ |
| |
| log.info("emqx http认证组件开始调用任务服务完成认证,clientid={},username={},password={}",clientid,username,password); |
| |
| String value = users.get(username); |
| if(StringUtils.isEmpty(value)){ |
| return new ResponseEntity(HttpStatus.UNAUTHORIZED); |
| } |
| |
| if(!value.equals(password)){ |
| return new ResponseEntity(HttpStatus.UNAUTHORIZED); |
| } |
| |
| return new ResponseEntity(HttpStatus.OK); |
| } |
| |
| } |
| |
- 使用MQTTX连接工具测试连接

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)