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

开启插件

在这里插入图片描述

二、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);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RedEric

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值