使用shiro标签遇到的坑

156 阅读1分钟

使用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、参考文档

shiro权限不生效原因分析_shiro放开接口不生效_bubble21的博客-CSDN博客