HTTP ACL的订阅和发布的代码

ACL文档参考:

发布订阅 ACL - HTTP ACL - 《EMQ X Broker v4.2 使用教程》 - 书栈网 · BookStack

代码实例:

 /**
     *  ACL权限认证
     *  主要是用来认证(发布和订阅消息)
     *  1.superuser 请求 :用于权限认证
     *  2.如果是超级管理员,则可以执行所以AIP接口
     * @param clientid
     * @param username
     * */
    @PostMapping("/superuser")
    public ResponseEntity superuserACL(@RequestParam("clientid") String clientid,@RequestParam("username") String username){
         if(clientid.equals(mqttEntity.getClient_id())&& username.equals(mqttEntity.getUsername()) ){
              log.info("---该用户是超级管理员(认证为--通过)----clientid:"+clientid+"usernam:"+username);
               return new ResponseEntity(HttpStatus.OK);
         }else {
             log.info("---该用户是普通用户(认证为(不)通过)----clientid:"+clientid+"usernam:"+username);
             return new ResponseEntity(HttpStatus.UNAUTHORIZED);
         }
    }
    /**
     * ACL权限认证
     * 1.这是实现多好权限认证的AIP接口
     * @param clientid Client ID
     * @param access 操作类型: 1 订阅 2发布
     * @param ipaddr  客户端IP地址
     * @param mountpoint MOuntpoint
     * @param topic  主题
     * @param username 客户端用户名
     * */
    @PostMapping("/acl")
    public ResponseEntity acl(@RequestParam("access") int access,@RequestParam("username") String username,
                              @RequestParam("clientid") String clientid ,@RequestParam("ipaddr") String ipaddr,
                              @RequestParam("topic") String topic  ,@RequestParam("mountpoint") String mountpoint

     ){
        //tests -tests ,只能订阅不能发布 只能订阅testtopic/123
        String value = stringRedisTemplate.opsForValue().get(username);
        if(!value.isEmpty()&& topic.equals("testtopic/#")&&access==1){
            log.info("tests--只订阅(testtopic/123)access="+access+"username="+username+"topic="+topic);
            return new ResponseEntity(HttpStatus.OK);
        }
        // admin 可以订阅和发布 testtopic/123
        if(!value.isEmpty()&& topic.equals("testtopic/123")){
            log.info("admin--可以订阅和发布(testtopic/123)access="+access+"username="+username+"topic="+topic);
            return new ResponseEntity(HttpStatus.OK);
        }
        log.info("username"+username+"没有权限 发布?订阅");
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);

    }

图片:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT空门:门主

你的鼓励是我发稿的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值