emq--使用外部自建 HTTP 应用认证

认证原理

EMQ X 在设备连接事件中使用当前客户端相关信息作为参数,向用户自定义的认证服务发起请求查询权限,通过返回的 HTTP 响应状态码 (HTTP statusCode) 来处理认证请求。

  • 认证失败:API 返回 4xx 状态码
  • 认证成功:API 返回 200 状态码
  • 忽略认证[由其它认证插件认证]:API 返回 200 状态码且消息体 ignore


1 线上使用建议关闭 匿名登陆
打开安装目录下的\etc\emqx.conf文件
修改 allow_anonymous = false
重启 emq ,即可
emqx restart 

2 修改http认证插件的配置文件 etc/plugins/emqx_auth_http.conf

# etc/plugins/emqx_auth_http.conf

## 启用 HTTPS 所需证书信息
## auth.http.ssl.cacertfile = etc/certs/ca.pem

## auth.http.ssl.certfile = etc/certs/client-cert.pem

## auth.http.ssl.keyfile = etc/certs/client-key.pem

## 请求头设置
## auth.http.header.Accept = */*

## 重试设置
auth.http.request.retry_times = 3

auth.http.request.retry_interval = 1s

auth.http.request.retry_backoff = 2.0

配置认证请求的url,提交方式,提交的参数等

进行身份认证时,EMQ X 将使用当前客户端信息填充并发起用户配置的认证查询请求,查询出该客户端在 HTTP 服务器端的认证数据。

# etc/plugins/emqx_auth_http.conf

## 请求地址
auth.http.auth_req = http://127.0.0.1:8991/mqtt/auth

## HTTP 请求方法
## Value: post | get | put
auth.http.auth_req.method = post

## 请求参数
auth.http.auth_req.params = clientid=%c,username=%u,password=%P

HTTP 请求方法为 GET 时,请求参数将以 URL 查询字符串的形式传递;POST、PUT 请求则将请求参数以普通表单形式提交(content-type 为 x-www-form-urlencoded)。

你可以在认证请求中使用以下占位符,请求时 EMQ X 将自动填充为客户端信息:

  • %u:用户名
  • %c:Client ID
  • %a:客户端 IP 地址
  • %r:客户端接入协议
  • %P:明文密码
  • %p:客户端端口
  • %C:TLS 证书公用名(证书的域名或子域名),仅当 TLS 连接时有效
  • %d:TLS 证书 subject,仅当 TLS 连接时有效

3 使用springboot,根据以上的配置文件,编写认证代码

package cn.huawei.emq_http_authority.controller;

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.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mqtt")
public class Controller_3 {

    private Map<String,String> users=null;

    @PostConstruct
    public void Init()
    {
        users=new HashMap<>();
        users.put("haha1","123456");
        users.put("haha2","123456");
        users.put("haha3","123456");
        users.put("vipuser","123456");
    }


    @PostMapping("/auth")
    public ResponseEntity diyYanzhen(String clientid,String username,String password)
    {
        String pwd = users.get(username);
        if(StringUtils.isEmpty(pwd))
        {
            return new ResponseEntity(HttpStatus.UNAUTHORIZED) ;
        }
        if(!pwd.equals(password))
        {
            return new ResponseEntity(HttpStatus.UNAUTHORIZED) ;
        }
        return new ResponseEntity(HttpStatus.OK);
    }

}

4 在emq的控制台启用 emqx_auth_http 插件
5 重启emq服务,开始测试

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值