zoukankan      html  css  js  c++  java
  • shiro(二)

    public class AuthorizerTest {
    
        @Test
        public void testIsPermitted() {
            login("classpath:shiro-authorizer.ini", "zhang", "123");
            //判断拥有权限:user:create
            Assert.assertTrue(subject().isPermitted("user1:update"));
            Assert.assertTrue(subject().isPermitted("user2:update"));
            //通过二进制位的方式表示权限
            Assert.assertTrue(subject().isPermitted("+user1+2"));//新增权限
            Assert.assertTrue(subject().isPermitted("+user1+8"));//查看权限
            Assert.assertTrue(subject().isPermitted("+user2+10"));//新增及查看
    
            Assert.assertFalse(subject().isPermitted("+user1+4"));//没有删除权限
    
            Assert.assertTrue(subject().isPermitted("menu:view"));//通过MyRolePermissionResolver解析得到的权限
        }
    
        protected void login(String configFile, String username, String password) {
            //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
            Factory<org.apache.shiro.mgt.SecurityManager> factory =
                    new IniSecurityManagerFactory(configFile);
    
            //2、得到SecurityManager实例 并绑定给SecurityUtils
            org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
            SecurityUtils.setSecurityManager(securityManager);
    
            //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    
            subject.login(token);//securityManager.login(this, token);
    
        }
    
    } 

    shiro-authorizer.ini

    [main]
    #自定义authorizer
    authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
    #自定义permissionResolver
    #permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
    permissionResolver=com.github.zhangkaitao.shiro.chapter3.permission.BitAndWildPermissionResolver
    authorizer.permissionResolver=$permissionResolver
    #自定义rolePermissionResolver
    rolePermissionResolver=com.github.zhangkaitao.shiro.chapter3.permission.MyRolePermissionResolver
    authorizer.rolePermissionResolver=$rolePermissionResolver

    securityManager.authorizer=$authorizer

    #自定义realm 一定要放在securityManager.authorizer赋值之后(因为调用setRealms会将realms设置给authorizer,并给各个Realm设置permissionResolver和rolePermissionResolver)
    realm=com.github.zhangkaitao.shiro.chapter3.realm.MyRealm
    securityManager.realms=$realm

    public class MyRealm extends AuthorizingRealm
    {
    	@Override
    	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) 
    	{
    	//授权,实际中从数据库中给用户访问页面的权限,从页面的操作都可以在这进行授权,如下面注释
    /*        String username = (String) principals.fromRealm(getName()).iterator().next();
            if (username != null) {
                User user = userService.getByUsername(username);
                if (user != null){
                    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
                    List<AuthMenu> menuList = userService.getMenuList(user.getId());
                    for (AuthMenu menu : menuList){
                        if (StringUtils.isNotBlank(menu.getPermission())){
                            // 添加基于Permission的权限信息
                            for (String permission : StringUtils.split(menu.getPermission(),",")){
                                info.addStringPermission(permission);
                            }
                        }
                    }*/
    		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    	    authorizationInfo.addRole("role1");
    	    authorizationInfo.addRole("role2");
    
    	    authorizationInfo.addObjectPermission(new WildcardPermission("user1:*"));
    	    authorizationInfo.addStringPermission("+user2+10");
    	    authorizationInfo.addStringPermission("user2:*");
            return authorizationInfo;
        }
    
    	@Override
    	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
    			throws AuthenticationException {
    		// 认证,subject.login时调用,一般验证用户名和密码
            String username = (String)token.getPrincipal();  //得到用户名
            String password = new String((char[])token.getCredentials()); //得到密码
            if(!"zhang".equals(username)) {
                throw new UnknownAccountException(); //如果用户名错误
            }
            if(!"123".equals(password)) {
                throw new IncorrectCredentialsException(); //如果密码错误
            }
            //如果身份认证验证成功,返回一个AuthenticationInfo实现;
            return new SimpleAuthenticationInfo(username, password, getName());
    	}
    
    }
    

      







    }

  • 相关阅读:
    mysql前缀索引的应用
    记博客园
    好的博客网站(随手记)
    memcache应对缓存失效问题
    memcache内存分配问题
    memcached使用libevent 和 多线程模式
    RabbitMQ用户及权限控制
    Nginx基础之常用配置
    PHP-fpm进程池优化方法
    php-fpm参数详解
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9650858.html
Copyright © 2011-2022 走看看