zoukankan      html  css  js  c++  java
  • shiro授权

    一、shiro-permission.ini

    shiro-permission.ini里面的内容相当于在数据库

    #用户
    [users]
    #用户zhang的密码是123,此用户具有role1和role2两个角色
    zhangsan=123,role1,role2
    wang=123,role2
    
    #权限
    [roles]
    #角色role1对资源user拥有create、update权限
    role1=user:create,user:update
    #角色role2对资源user拥有create、delete权限
    role2=user:create,user:delete
    #角色role3对资源user拥有create权限
    role3=user:create

    权限标识符号规则:资源:操作:实例(中间使用半角:分隔)

    usercreate:01  表示对用户资源的01实例进行create操作。

    user:create:表示对用户资源进行create操作,相当于user:create:*,对所有用户资源实例进行create操作。

    user*01  表示对用户资源实例01进行所有操作。

    // 角色授权、资源授权测试
        @Test
        public void testAuthorization() {
    
            // 创建SecurityManager工厂
            Factory<SecurityManager> factory = new IniSecurityManagerFactory(
                    "classpath:shiro-permission.ini");
    
            // 创建SecurityManager
            SecurityManager securityManager = factory.getInstance();
    
            // 将SecurityManager设置到系统运行环境,和spring后将SecurityManager配置spring容器中,一般单例管理
            SecurityUtils.setSecurityManager(securityManager);
    
            // 创建subject
            Subject subject = SecurityUtils.getSubject();
    
            // 创建token令牌
            UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
                    "123");
    
            // 执行认证
            try {
                subject.login(token);
            } catch (AuthenticationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            System.out.println("认证状态:" + subject.isAuthenticated());
            // 认证通过后执行授权
    
            // 基于角色的授权
            // hasRole传入角色标识
            boolean ishasRole = subject.hasRole("role1");
            System.out.println("单个角色判断" + ishasRole);
            // hasAllRoles是否拥有多个角色
            boolean hasAllRoles = subject.hasAllRoles(Arrays.asList("role1",
                    "role2", "role3"));
            System.out.println("多个角色判断" + hasAllRoles);
    
            // 使用check方法进行授权,如果授权不通过会抛出异常
            // subject.checkRole("role13");
    
            // 基于资源的授权
            // isPermitted传入权限标识符
            boolean isPermitted = subject.isPermitted("user:create:1");
            System.out.println("单个权限判断" + isPermitted);
    
            boolean isPermittedAll = subject.isPermittedAll("user:create:1",
                    "user:delete");
            System.out.println("多个权限判断" + isPermittedAll);
    
            // 使用check方法进行授权,如果授权不通过会抛出异常
            subject.checkPermission("user:create:1");//不抛
            subject.checkPermission("item:create:1");//抛异常
    
        }

    自定义realm进行授权

    实际开发中从数据库中获取权限数据。就需要自定义realm,由realm从数据库查询权限数据。

    realm根据用户身份查询权限数据,将权限数据返回给authorizer(授权器)。

    public class CustomRealm extends AuthorizingRealm {
    
        // 设置realm的名称
        @Override
        public void setName(String name) {
            super.setName("customRealm");
        }
    
        // 用于认证
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(
                AuthenticationToken token) throws AuthenticationException {
    
            // token是用户输入的
            // 第一步从token中取出身份信息
            String userCode = (String) token.getPrincipal();
    
            // 第二步:根据用户输入的userCode从数据库查询
            // ....
        
    
            // 如果查询不到返回null
            //数据库中用户账号是zhangsansan
            /*if(!userCode.equals("zhangsansan")){//
                return null;
            }*/
            
            
            // 模拟从数据库查询到密码
            String password = "111111";
    
            // 如果查询到返回认证信息AuthenticationInfo
    
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    userCode, password, this.getName());
    
            return simpleAuthenticationInfo;
        }
    
        // 用于授权
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(
                PrincipalCollection principals) {
            
            //从 principals获取主身份信息
            //将getPrimaryPrincipal方法返回值转为真实身份类型(在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型),
            String userCode =  (String) principals.getPrimaryPrincipal();
            
            //根据身份信息获取权限信息
            //连接数据库...
            //模拟从数据库获取到数据
            List<String> permissions = new ArrayList<String>();
            permissions.add("user:create");//用户的创建
            permissions.add("items:add");//商品添加权限
            //....
            
            //查到权限数据,返回授权信息(要包括 上边的permissions)
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            //将上边查询到授权信息填充到simpleAuthorizationInfo对象中
            simpleAuthorizationInfo.addStringPermissions(permissions);
    
            return simpleAuthorizationInfo;
        }
    
    }

    shiro-realm.ini

    在shiro-realm.ini中配置自定义的realm,将realm设置到securityManager中。

    [main]
    #自定义 realm
    customRealm=cn.itcast.shiro.realm.CustomRealm
    
    securityManager.realms=$customRealm

    测试程序:

    // 自定义realm进行资源授权测试
        @Test
        public void testAuthorizationCustomRealm() {
    
            // 创建SecurityManager工厂
            Factory<SecurityManager> factory = new IniSecurityManagerFactory(
                    "classpath:shiro-realm.ini");
    
            // 创建SecurityManager
            SecurityManager securityManager = factory.getInstance();
    
            // 将SecurityManager设置到系统运行环境,和spring后将SecurityManager配置spring容器中,一般单例管理
            SecurityUtils.setSecurityManager(securityManager);
    
            // 创建subject
            Subject subject = SecurityUtils.getSubject();
    
            // 创建token令牌
            UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
                    "111111");
    
            // 执行认证
            try {
                subject.login(token);
            } catch (AuthenticationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            System.out.println("认证状态:" + subject.isAuthenticated());
            // 认证通过后执行授权
    
            // 基于资源的授权,调用isPermitted方法会调用CustomRealm从数据库查询正确权限数据
            // isPermitted传入权限标识符,判断user:create:1是否在CustomRealm查询到权限数据之内
            boolean isPermitted = subject.isPermitted("user:create:1");
            System.out.println("单个权限判断" + isPermitted);
    
            boolean isPermittedAll = subject.isPermittedAll("user:create:1",
                    "user:create");
            System.out.println("多个权限判断" + isPermittedAll);
    
            // 使用check方法进行授权,如果授权不通过会抛出异常
            subject.checkPermission("items:add:1");
    
        }
  • 相关阅读:
    codechef: ADAROKS2 ,Ada Rooks 2
    codechef: BINARY, Binary Movements
    codechef : TREDEG , Trees and Degrees
    ●洛谷P1291 [SHOI2002]百事世界杯之旅
    ●BZOJ 1416 [NOI2006]神奇的口袋
    ●CodeForce 293E Close Vertices
    ●POJ 1741 Tree
    ●CodeForces 480E Parking Lot
    ●计蒜客 百度地图的实时路况
    ●CodeForces 549F Yura and Developers
  • 原文地址:https://www.cnblogs.com/zailushang1996/p/8625382.html
Copyright © 2011-2022 走看看