1 , shiro
1.1 Authorization Process
1.2 Three authorization methods
Shiro supports three ways of authorization:
Programmatically: done by writing an if/else authorization block:
Subject subject = SecurityUtils.getSubject();
if(subject.hasRole(“admin”)) {
// have permission
} else {
// no permission
}
Annotated: done by placing the corresponding annotation on the executed Java method:
@RequiresRoles("admin")
public void hello() {
// have permission
}
JS P/GSP tags: Completed by corresponding tags on JSP/GSP pages:
<shiro:hasRole name="admin">
<! — has permission — >
</shiro:hasRole>
1.3 shiro-permission.ini
The content in shiro-permission.ini is equivalent to the database.
#user _
[users]
#User zhang 's password is 123 , this user has two roles , role1 and role2
zhang = 123,role1,role2
wang = 123,role2
#Permissions _
[roles]
# role1 has create and update permissions for resource user
role1=user:create,user:update
# role2 has create and delete permissions for resource user
role2=user:create,user:delete
# role3 has create permission to resource user
role3=user:create
Permission identification symbol rules: resource : operation : instance ( use half-width : separation in the middle )
user : create:01 indicates that the create operation is performed on the 01 instance of the user resource .
user:create : Indicates that the create operation is performed on user resources, which is equivalent to user:create:* , and the create operation is performed on all user resource instances .
user : * : 01 means perform all operations on user resource instance 01 .
Programming
2 Customize realm for authorization
2.1 Requirements
The above program statically configures the permission information through shiro-permission.ini , and obtains the permission data from the database in actual development. You need to customize the realm , and the realm will query the permission data from the database.
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串")
2、SecurityManager执行授权,通过ModularRealmAuthorizer执行授权
3、ModularRealmAuthorizer执行realm(自定义的CustomRealm)从数据库查询权限数据
调用realm的授权方法:doGetAuthorizationInfo
4、realm从数据库查询权限数据,返回ModularRealmAuthorizer
5、ModularRealmAuthorizer调用PermissionResolver进行权限串比对
6、如果比对后,isPermitted中"permission串"在realm查询到权限数据中,说明用户访问permission串有权限,否则 没有权限,抛出异常。