一、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
开启插件
二、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); } }