zoukankan      html  css  js  c++  java
  • shiro认证通过之后的授权

    subject.hasRole("") ;

    subject.hasRoles(List);

    subject.hasAllRoles();

    subject.isPermitted("");

    修改ini文件

    #配置用户名
    [users]
    zhangsan=123456,role1
    lisi=123456,role2
    wanghu=123456,role3
    zhaoliu=123456,role2,role3
    sunqi=123456,role4

    #声明角色
    [roles]
    role1=user:query,user:add,user:update,user:delete,user:export
    role2=user:query,user:add
    role3=user:query,user:export
    role4=*:*
        //日志输出工具
        private static final transient Logger log = LoggerFactory.getLogger(TestAuthenticationApp.class);
        public static void main(String[] args) {
            
             String username = "zhangsan";
             String password = "123456";
            
             log.info("My First Apache Shiro Application");
             //1 创建安全管理器的工厂对象
             Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
             //2 使用工厂创建安全管理器
             SecurityManager securityManager = factory.getInstance();
             //3 把当前的安全管理器绑定到线程
             SecurityUtils.setSecurityManager(securityManager);
             //4 使用SecurityUtils.getSubject() 得到主体
             Subject currentUser = SecurityUtils.getSubject();
            //5 封装用户名
             AuthenticationToken arg0 = new UsernamePasswordToken(username, password);
             currentUser.login(arg0);
             System.out.println("认证通过");
             
             //退出的方法
             //currentUser.logout();
             
             //判断用户是否通过认证
             boolean authent = currentUser.isAuthenticated();
             System.out.println(authent);
             
             //角色判断
             boolean hasrole1 = currentUser.hasRole("role1");
             System.out.println(hasrole1);
             //分别判断集合里面的角色返回数组
             List<String> arr = Arrays.asList("role1","role2","role3");
             boolean[] arrs = currentUser.hasRoles(arr);
             for (boolean string : arrs) {
                System.out.println(string);
            }
             
             //判断当前用户是否有 arr集合里的所有角色
             boolean hasAllRoles = currentUser.hasAllRoles(arr);
             System.out.println(hasAllRoles);
             
             //权限判断
             boolean permitted = currentUser.isPermitted("user:query");
             System.out.println("当前用户有当前权限吗"+permitted);
                 //分别判断
             boolean[] permitted2 = currentUser.isPermitted("user:query","user:add");
    
                 //同时判断
             boolean permitted3 = currentUser.isPermittedAll("user:query","user:add");
  • 相关阅读:
    js:通过正则匹配获取页面的url中的参数
    BZOJ 4873 寿司餐厅 网络流
    BZOJ 3308 毒瘤结论 网络流
    BZOJ3451 CDOJ1562点分治+FFT
    P3731 二分图匹配必经边
    Hdu 6268 点分治 树上背包 bitset 优化
    Hdu 1517 巴什博奕变形
    dsu on tree
    CF #546 D.E
    HNOI 世界树 虚树
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/12040122.html
Copyright © 2011-2022 走看看