shiro认证

1、shiro授权

1.1 授权流程

1.2 三种授权方法

 Shiro 支持三种方式的授权:

编程式:通过写if/else 授权代码块完成:

Subject subject = SecurityUtils.getSubject();

if(subject.hasRole(“admin”)) {

//有权限

} else {

//无权限

}

注解式:通过在执行的Java方法上放置相应的注解完成:

@RequiresRoles("admin")

public void hello() {

//有权限

}

JSP/GSP 标签:在JSP/GSP 页面通过相应的标签完成:

<shiro:hasRole name="admin">

<!— 有权限—>

</shiro:hasRole>

 

1.3 shiro-permission.ini

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

 

#用户

[users]

#用户zhang的密码是123,此用户具有role1role2两个角色

zhang=123,role1,role2

wang=123,role2

 

#权限

[roles]

#角色role1对资源user拥有createupdate权限

role1=user:create,user:update

#角色role2对资源user拥有createdelete权限

role2=user:create,user:delete

#角色role3对资源user拥有create权限

role3=user:create

 

 

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

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

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

 

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

 

程序编写

 

 

自定义realm进行授权

2.1 需求

上边的程序通过shiro-permission.ini对权限信息进行静态配置,实际开发中从数据库中获取权限数据。就需要自定义realm,由realm从数据库查询权限数据。

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

 

2.2 自定义realm

 

在原来自定义的realm中,修改doGetAuthorizationInfo方法。

 

 

2.3 shiro-realm.ini

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

 

[main]

#自定义 realm

customRealm=cn.itcast.shiro.realm.CustomRealm

#realm设置到securityManager,相当 于spring中注入

securityManager.realms=$customRealm

 

2.4 测试程序

 

// 自定义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");

	}

 

2.5 授权流程

 

1、对subject进行授权,调用方法isPermitted"permission"

2SecurityManager执行授权,通过ModularRealmAuthorizer执行授权

3ModularRealmAuthorizer执行realm(自定义的CustomRealm)从数据库查询权限数据

调用realm的授权方法:doGetAuthorizationInfo

 

4realm从数据库查询权限数据,返回ModularRealmAuthorizer

5ModularRealmAuthorizer调用PermissionResolver进行权限串比对

6、如果比对后,isPermitted"permission"realm查询到权限数据中,说明用户访问permission串有权限,否则 没有权限,抛出异常。

猜你喜欢

转载自liweirr789.iteye.com/blog/2398560