zoukankan      html  css  js  c++  java
  • shiro 角色认证授权,权限认证授权

    首先通过用户登录获取到Subject对象,通过里面的一些方法来判断用户的角色.

    1.判断是否拥有该角色,返回boolean值

    subject.hasRole("role2") 返回一个boolean型
    subject.hasRoles(List<String> list)返回一个boolean型数组,通过循环对面一个角色进行判断
    
    
    subject.hasAllRoles(List<String> list)返回一个boolean型,判断用户是否拥有所有角色
    
    
    检测是否拥有该角色,如果没有直接抛出异常
    
    
    subject.checkRole("role1");
    subject.checkRoles(Arrays.asList("role1","role2"));
    subject.checkRoles("role1","role2");


    2.权限验证:返回boolean值,用法和角色认证一样

    
    
    subject.isPermitted(String str);
    
    
    subject.isPermitted(String...strings);
    
    
    subject.isPermittedAll(String...strings);
    
    
    3.新建.ini文件,建立一个角色的.ini和一个权限的.ini文件
    
    
    
    

    
    
    左边表示用户,右边第一个表示密码,后面就表示该用户所拥有的角色
    
    
    
    

    
    
    上面的users表示用户和角色,下面是设置该角色所拥有的权限

    4.添加一个junit包

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

    5.新建一个测试类

     /**
      * 角色认证授权-HasRole
      */
    @Test
    public void roleHasRole(){
        Subject subject = login("classpath:shiro_role.ini","spf","123456");
         System.out.println(subject.hasRole("role2")?"有role2角色":"没有role2角色");
    
         boolean[] flags = subject.hasRoles(Arrays.asList("role1","role2"));
         System.out.println(flags[0]?"有role1角色":"没有role1角色");
         System.out.println(flags[1]?"有role2角色":"没有role2角色");
    
         System.out.println(subject.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2两个角色都有":"role1,role2两个角色不全有");
    
         subject.logout();
    }
    
     /**
      * 角色认证授权-CheckRole
      * 认证不成功就抛出异常
      */
    @Test
    public void roleCheckRole(){
        Subject subject = login("classpath:shiro_role.ini","spf","123456");
        subject.checkRole("role1");
        subject.checkRoles(Arrays.asList("role1","role2"));
        subject.checkRoles("role1","role2");
        subject.logout();
    }
    
     /**
      * 权限认证授权-isPermitted
      */
    @Test
     public void rolePermitted(){
         Subject subject = login("classpath:shiro_permitted.ini","spf","123456");
         //Subject subject = login("classpath:shiro_permitted.ini","jack","123");
    System.out.println(subject.isPermitted("update")?"有update这个权限":"没有update这个权限");
    
         boolean[] flags = subject.isPermitted("select","update");
         System.out.println(flags[0]?"有select这个权限":"没有select这个权限");
         System.out.println(flags[1]?"有update这个权限":"没有update这个权限");
    
         System.out.println(subject.isPermittedAll("select","update")?"有select,update这两个权限":"select,update这两个权限不全有");
    
         subject.logout();
     }
    
     private Subject login(String url, String username, String password){
         // 读取配置文件,初始化SecurityManager工厂
    Factory<SecurityManager> factory = new IniSecurityManagerFactory(url);
         // 获取securityManager实例
    SecurityManager securityManager=factory.getInstance();
         // 把securityManager实例绑定到SecurityUtils
    SecurityUtils.setSecurityManager(securityManager);
         // 得到当前执行的用户
    Subject subject = SecurityUtils.getSubject();
         // 创建token令牌,用户名/密码
    UsernamePasswordToken token=new UsernamePasswordToken(username, password);
         try {
             // 身份认证
    subject.login(token);
             System.out.println("身份认证成功");
         } catch (AuthenticationException e) {
             e.printStackTrace();
             if (e instanceof IncorrectCredentialsException) {
                 throw new IncorrectCredentialsException("密码错误");
             } else if (e instanceof UnknownAccountException) {
                 throw new UnknownAccountException("用户名错误");
             }
         }
         return subject;
     }


    感谢该文章的作者,作者的链接https://blog.csdn.net/qq_32331997/article/details/77978041
  • 相关阅读:
    php 判断访问是否是手机或者pc
    SQLSTATE[HY000] [2002] No such file or directory
    No input file specified.
    Call to undefined function openssl_decrypt()
    Parse error: syntax error, unexpected 'class' (T_CLASS)
    tp5关联模型进行条件查询
    windows下php7.1安装redis扩展以及redis测试使用全过程
    SourceTree跳过初始设置
    对象数组(JSON) 根据某个共同字段 分组
    SDUT 3377 数据结构实验之查找五:平方之哈希表
  • 原文地址:https://www.cnblogs.com/wwqqnn123456/p/9485691.html
Copyright © 2011-2022 走看看