Emqx配置HTTP鉴权集成springboot

简介: Emqx配置HTTP鉴权集成springboot

一、emqx添加http登录鉴权认证

进入emqx映射出来的配置文件目录,编辑鉴权的配置文件

cd /home/egn/emqx/etc/plugins
vim emqx_auth_http.conf
auth.http.auth_req.url = http://127.0.0.1:8002/emqapi/auth【修改接口地址】
auth.http.super_req.url = http://127.0.0.1:8003/emqapi/superuser【放开注释并修改接口地址】
auth.http.super_req.method = post【放开注释】
auth.http.super_req.headers.content-type = application/x-www-form-urlencoded【放开注释】
auth.http.super_req.params = clientid=%c,username=%u【放开注释】【修改接口地址】
auth.http.acl_req.url = http://127.0.0.1:8003/emqapi/acl【修改接口地址】

重启容器,使配置文件生效

docker restart emqx

开启插件

588ace8414484858bb16f646dc2e7c1d.png

二、Springboot 鉴权接口

@RestController
@RequestMapping("/emqapi")
@Api(value="EMQAPI接口",tags={"EMQAPI接口"})
public class EmqApiController{
  private static Logger logger = LoggerFactory.getLogger(EmqApiController.class);
  @Autowired
  AuthService authService;
  @ApiOperation(value = "客户端连接授权" ,notes = "客户端连接授权" )
  @ApiImplicitParams({
      @ApiImplicitParam(name = "clientid" ,value = "客户端clientId" , required = false, dataType = "String"),
      @ApiImplicitParam(name = "username" ,value = "客户端username" , required = false, dataType = "String"),
      @ApiImplicitParam(name = "password" ,value = "客户端password" , required = false, dataType = "String")
  })
  @PostMapping(value = "/auth")
  public void checkUser(String clientid, String username, String password, HttpServletResponse response) throws Exception {
    //这里添加业务逻辑 200 允许登录,其他Stuatus不允许登录
        System.out.println("普通用户;clientid:" + clientid + ";username:" + username);
    response.setStatus(200);
  }
  @PostMapping("/superuser")
  public void mqttSuperuser(String clientid, String username, HttpServletResponse response) {
    logger.info("超级用户;clientid:" + clientid + ";username:" + username);
    System.out.println("超级用户;clientid:" + clientid + ";username:" + username);
    response.setStatus(200);
  }
  @PostMapping("/acl")
  public void mqttAcl(String access, String username, String clientid, String ipaddr, String topic, HttpServletResponse response) {
    //auth.http.acl_req.params = access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t
    logger.info("access: " + access + ";username: " + username + ";clientid: " + clientid + "; ipaddr: " + ipaddr + ";topic: " + topic);
    System.out.println("access: " + access + ";username: " + username + ";clientid: " + clientid + "; ipaddr: " + ipaddr + ";topic: " + topic);
    response.setStatus(200);
  }
}


目录
相关文章
|
6天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
12天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
45 0
|
13天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
53 2
|
1月前
|
Web App开发 监控 Java
|
17天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
153 0
|
24天前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
204 1
|
1月前
|
XML Java 关系型数据库
【SpringBoot系列】SpringBoot集成Fast Mybatis
【SpringBoot系列】SpringBoot集成Fast Mybatis
|
1月前
File和FTP和HTTP软件配置
File和FTP和HTTP软件配置