使用shiro标签遇到的坑

1、前言

使用shiro框架做权限控制是不错的选择,在使用shiro标签的过程中,是否会遇到一些坑呢。恰好笔者遇到了,在此记录一下。

2、shiro标签坑的描述

标签:

<shiro:haspermission name="你的权限"></shiro:haspermission>

当使用的标签具有层级关系的时候,就有可能掉坑里,并且出不来。

例如:

第一层级权限为:【dealer:clue】,第二层级为:【dealer:clue:add】,这个使用坑就出现了。当你使用【dealer:clue:add】权限,关闭【dealer:clue】权限时,你会发现然并卵。

查了很多资料,有人说shiro标签具有继承关系,其实不是的。是因为shiro框的判断机制引起的。我们来一起看看源码吧。

3、源码追踪

标签调用后台的方法:

核心方法:

protected List<Set<String>> getParts() {
    return this.parts;
}

public boolean implies(Permission p) {
    if (!(p instanceof WildcardPermission)) {
	return false;
    } else {
        WildcardPermission wp = (WildcardPermission) p;
        // 将权限标识转化成set集合
        List otherParts = wp.getParts();
        int i = 0;

        for (Iterator part = otherParts.iterator(); part.hasNext(); ++i) {
	    Set otherPart = (Set) part.next();
	    if (this.getParts().size() - 1 < i) {
	        return true;
	    }

	    Set part1 = (Set) this.getParts().get(i);
            // 判断包含关系【dealer:clue:add】与【dealer:clue】
	    if (!part1.contains("*") && !part1.containsAll(otherPart)) {
	    	return false;
	    }
        }

        while (i < this.getParts().size()) {
	    Set arg7 = (Set) this.getParts().get(i);
	    if (!arg7.contains("*")) {
	        return false;
	    }

	    ++i;
        }

        return true;
    }
}

4、总结

综上所述,有层级关系的权限标识就有可能带来使用的误区。

shiro标签的判断方式是通过集合的包含关系判断的,并不是通过字符串的形式。

5、参考文档

https://blog.csdn.net/bubble21/article/details/78829959

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值