角色:角色代表了操作集合,可以理解为权限的集合,一般情况下我们会赋予用户角色而不是权限,即这样用户可以拥有一组权限,赋予权限时比较方便。典型的如:项目经理、技术总监、CTO、开发工程师等都是角色,不同的角色拥有一组不同的权限。

隐式角色:即通过角色直接控制用户的权限;

显式角色:在程序中通过权限控制谁能访问某个资源,角色聚合一组权限集合;这样假设哪个角色不能访问某个资源,只需要从角色代表的权限集合中移除即可;无须修改多处代码;即粒度是以资源/实例为单位的;粒度较细。

 

一、授权方式

 

if(subject.hasRole("role1")){
				System.out.println("拥有权限"+"role1");
			}else{
				System.out.println("无权限"+"role1");
			}

二、在ini配置文件中配置用户拥有的角色

 

 

#用户名 密码,角色
[users]
zhang=123,role1,role2

规则即:“用户名=密码,角色1,角色2”,如果需要在应用中判断用户是否有相应角色,就需要在相应的Realm中返回角色信息,也就是说Shiro不负责维护用户-角色信息,需要应用提供,Shiro只是提供相应的接口方便验证;

 

 

 

三、在ini文件中配置用户角色及角色拥有的权限

#用户名 密码,角色
[users]
zhang=123,role1,role2
[roles]
role1:user:create,user:update
role2:user:create,user:delete

规则:“用户名=密码,角色1,角色2”“角色=权限1,权限2”,即首先根据用户名找到角色,然后根据角色再找到权限;即角色是权限集合;Shiro同样不进行权限的维护,需要我们通过Realm返回相应的权限信息。只需要维护“用户——角色”之间的关系即可。

 

 

 

四、验证用户的角色及角色拥有拥有的权限

 

package shiro;

import java.util.Arrays;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http://jinnianshilongnian.iteye.com/blog/2019547
 * @Description:TODO
 * @version 1.0
 * @since JDK1.7
 * @author yaomy
 * @company xxxxxxxxxxxxxx
 * @copyright (c) 2017 yaomy Co'Ltd Inc. All rights reserved.
 * @date 2017年9月13日 下午4:37:09
 */
public class RealmRoleDemo {
	private static final transient Logger log = LoggerFactory.getLogger(RealmRoleDemo.class);
	public static void main(String[] args) {
		//获取SecurityManager安全管理器工厂类,此处使用shiro.ini文件进行初始化
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("conf/shiro-role.ini");
		//获取SecurityManager安全管理器实例,并绑定给SecurityUtils
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		
		//获取主题
		Subject subject = SecurityUtils.getSubject();
		
		//创建用户名密码身份验证token(即:用户身份/凭证)
		UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123", true);
		try{
			//登录,即身份验证
			subject.login(token);
			log.info("登录成功");
			if(subject.hasRole("role1")){
				System.out.println("拥有权限"+"role1");
			}else{
				System.out.println("无权限"+"role1");
			}
			if(subject.isPermitted("user:delete")){
				System.out.println("拥有删除的权限");
			}else{
				System.out.println("无删除的权限");
			}
			if(subject.isPermittedAll("user:create","user:update")){
				System.out.println("拥有更新,新增的权限");
			}else{
				System.out.println("无更新,新增的权限");
			}
			System.out.println(subject.getPrincipals());
			System.out.println(subject.getPrincipals().asList().size());
		} catch(AuthenticationException e){
			log.info("身份验证失败"+e);
		}
		
		subject.logout();
	}
}

执行结果

 

 

20:44:50,293 INFO  ~ Enabling session validation scheduler...
20:44:50,379 INFO  ~ 登录成功
拥有权限role1
拥有删除的权限
拥有更新,新增的权限
zhang
1