EMQX认证回调

0 阅读1分钟

前言

emqx可以做连接认证回调验证

http回调

文档为

https://docs.emqx.com/zh/emqx/latest/guides/access-control/authn/http.html

emqx密码认证回调

1、访问emqx界面

http://localhost:18083/

image.png 登录进去,默认账号是"admin",密码为"public"

2、选择Authentication菜单

image.png

3、点击Create按钮

image.png 出现

image.png 默认选择Password-Base

image.png 点击Next

image.png url填写为http://127.0.0.1:8080/hello,看自己服务器的回调地址,这里新增一个clientid

image.png 点击Create按钮,创建之后为

image.png 如果服务没启动的话,就会显示

写一个回调接口

@RestController
@RequestMapping("/hello")
public class HelloController {

    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @PostMapping
    public HelloResponse hello(@RequestBody AuthRequestBody body) {
        // 日志不打印密码明文,只打印长度
        log.info("[hello] clientid={}, username={}, passwordLen={}",
                body.clientid(), body.username(),
                body.password() == null ? 0 : body.password());
        return new HelloResponse("Hello, " + body.username() + "!", body.clientid(), body.username(), Instant.now());
    }

    /**
     * 请求体:字段名与 EMQX body 模板占位符一一对应。
     */
    public record AuthRequestBody(String clientid, String password, String username) {
    }

    public record  SHelloResponse(String message, String clientid, String username, Instant receivedAt) {
    }
}

image.png 然后客户端连接之后

image.png 连接之后,便会携带参数到接口认证

image.png

总结

可以使用emqx做回调认证