Apache Shiro教程(4)

shiro授权

  1. 授权

授权,即访问控制,控制谁能访问哪些资源,主体进行身份认证后需要分配权限方可访问系统的资源,对于某些资源没有权限是无法访问的。

  1. 关键对象

授权可简单理解为 who 对 what 进行how操作

  1. 授权流程
    20220903184936.png

授权方式

基于角色的访问控制 : RBAC基于角色的访问控制是以角色为中心进行控制
基于资源的访问控制 : RABC基于资源的访问控制是以资源为中心进行访问控制

  1. 权限字符串

权限字符串的规则: 资源标识符: 操作:资源实例标识符,意思是对哪些资源的那个实例有哪些具体的操作,“:” 是资源/操作/实例的分割符,权限字符串也可以使用* 通配副。

1、用户创建权限: user:create,或者user:create:*
2、用户修改实例001的权限:user:update:001
3、用户实例001的所有权限:user:*:001

  1. shiro中授权编码实现方式

1、角色

 if(subjcet.hasRole("admin")){
 
 }

2、资源

  if(subject.isPermission("user:create:001")){// 具体实例
  
}

  if(subject.isPermission("user:create:*")){// 资源类型
  
}
  1. 实现

1、编程

 if (subjcet.hasRole("admin")){
   
   }

2、注解

@RequestRoles("admin")
public void realmHello(){
}

    @RequiresRoles("admin")
    @RequiresPermissions("user:create:001")
    @RequestMapping("/user/test")
    public String userTest(){
        return "user/test";
    }


3、标签式

<shiro:hasRole name="admin">
</shiro:hasRole>

上述标签为jsp类型的实现,如果想用threamleaf thmeleaf的集成需要额外的部分
  1. 具体实现

TestAuthorizingRealmMd5中添加 如下代码:

       // 2、封装自定义 realm
        AuthorizingRealmMd5Customer authorizingRealmMd5Customer = new AuthorizingRealmMd5Customer();
            // 2.1、设置md5加密匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
            // 2.1、设置 散列次数
        credentialsMatcher.setHashIterations(12);

        authorizingRealmMd5Customer.setCredentialsMatcher(credentialsMatcher);

// =================================================================

        // 添加授权的功能
        if (subject.isAuthenticated()){
            //基于角色的开发
            if(subject.hasRole("admin")){
                System.out.println(subject.hasRole("admin"));
            }
            //基于资源的开发
            if (subject.isPermitted("user:create:001")){
                System.out.println("subject:=:"+subject.isPermitted("user:*:*"));
                System.out.println("subject:=:"+subject.isPermitted("user:create:001"));;
            }
        }

20220903211814.png

AuthorizingRealmMd5Customer 添加代码

        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Object primaryPrincipal = principalCollection.getPrimaryPrincipal();
        //根据用户名priaryPrincipal 查询数据库 查询相关的角色及资源信息

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRole("admin");
        simpleAuthorizationInfo.addRole("user");
        simpleAuthorizationInfo.addStringPermission("user:create:001");
        return simpleAuthorizationInfo;
    }

  1. 运行结果
subject:=:false
21:21:17.911 [main] DEBUG org.apache.shiro.realm.AuthorizingRealm - No authorizationCache instance set.  Checking for a cacheManager...
21:21:17.911 [main] DEBUG org.apache.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.
subject:=:true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值