shiro:hasPermission 标签重写支持多个权限

直接切入主题
事前准备 shiro单个权限能正常使用的情况下 :

1 重写AuthorizingRealm子类的isPermitted方法

private static final String OR_OPERATOR = " OR ";
private static final String AND_OPERATOR = " AND ";
@Override
public boolean isPermitted(PrincipalCollection principals, String permission) {
//return super.isPermitted(principals,permission);
    if (permission.contains(OR_OPERATOR)) {
//如果有任何一个权限,返回true,否则返回false
String[] permissions = permission.split(OR_OPERATOR);
for (String p : permissions) {
//只要有一个权限是通过验证的就返回true
if (super.isPermitted(principals,p)) {
return true;
}
}
return false;

}else if (permission.contains(AND_OPERATOR)) {

String[] permissions = permission.split(AND_OPERATOR);
for (String p : permissions) {
//只要有一个权限不是true的,我们就返回假
if (!super.isPermitted(principals,p)) {
return false;
}
}
return true;

}else{//不包含 OR AND 的走原来逻辑
return super.isPermitted(principals,permission);
}
}

   2.页面

 

posted @ 2021-04-14 18:46  agnils  阅读(2418)  评论(0编辑  收藏  举报