五、Shiro授权
5.1、授权
授权,即访问控制,控制谁能访问哪些资源。主体进行身份认证后需要分配权限方可访问系统的资源,对于某些资源设有权限是无法访问的。
5.2、关键对象
授权可简单理解为who对What(Which)进行How操作:
- Who即主体(Subject),主体需要访问系统中的资源。
- What即资源(Resource),如系统菜单、页面、按钮、类方法、系统商品信息等。资源包括资源类型和资源实例,比如
商品信息为资源类型
,类型为t01的商品为了资源实例
,编号为001的商品信息也属于资源实例。 - How权限/许可(Permission),规定了主体对资源的操作许可,权限离开资源没有意义,如用户查询权限、用户添加权限、某个类方法的调用权限、编号为001用户的修改权限等,通过权限可知主体对哪些资源都有那些许可。
5.3、授权流程
5.4、授权方式
-
基于角色的控制
-
RBAC基于角色的访问控制(Role-Based Access Control)是以角色为中心进行访问控制
if(subject.hasRole("admin")){ //操作什么资源 }
-
-
基于资源的访问控制
-
RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制
if(subject.isPermission("user:update:01")){ //资源实例 //对01用户进行修改 } if(subject.isPermission("user:update:*")){ //资源类型 //对01用户进行修改 }
-
5.5、权限字符串
权限字符串的规则是:资源标识符
:操作
:资源实例标识符
,意思是对哪个实例具有什么操作,==:是资源/操作/实例的分隔符,权限字符串也可以使用*==通配符。
例子:
- 用户创建权限:
user:create
,或user:create.*
- 用户修改实例001的权限:
user:update:001
- 用户实例001的所有权限:
user:*:001
5.6、shiro授权编程实现
编程式
Subject subject=SecurityUtils.getSubject();
if(subject.hasRole("admin")){
//有权限
}else{
//无权限
}
注解式
@RequiresRoles("admin")
public void hello(){
//有权限
}
标签式
#JSP/GSP,标签:在JSP/GSP页面通过相应的标签完成:
<shir:hasRole name="admin">
<!--有权限-->
</shiro:hasRole>
#注意:Thymeleaf中使用shiro需要额外集成
5.7、开发授权
基于角色进行控制
修改CustmerMD5Realm
package org.hz52.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
/**
* @Program: shiro_demo
* @Description: 使用自定义realm加入md5+salt+hash
* @Author: 52Hz
* @CreationTime: 2021年10月18日 9:25 星期一
**/
public class CustmerMD5Realm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String primaryPrincipal = (String) principals.getPrimaryPrincipal();
System.out.println("身份信息" + primaryPrincipal);
//根据身份信息 用户名 货当前用户的角色信息 ,以及权限信息 xiaochen admin users
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//将数据库中的查询角色信息赋值给权限对象
simpleAuthorizationInfo.addRole("admin");
simpleAuthorizationInfo.addRole("user");
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//获取身份信息
String principal = (String) token.getPrincipal();
//根据用户名查询数据库
if ("xiaochen".equals(principal)) {
//参数1:数据库用户名 参数2:数据库MD5+Salt之后的密码 参数3:注册
return new SimpleAuthenticationInfo(principal,
"6abca58de7461f2b5d795d3a6e79aaa6",
ByteSource.Util.bytes("X0#31"),
this.getName());
}
return null;
}
}java
修改testCustmerMD5Authenticator
package org.hz52;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.hz52.realm.CustmerMD5Realm;
import java.util.Arrays;
/**
* @Program: shiro_demo
* @Description:
* @Author: 52Hz
* @CreationTime: 2021年10月18日 9:27 星期一
**/
public class testCustmerMD5Authenticator {
public static void main(String[] args) {
//1、创建安全管理器
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//2、注入realm
//设置realm使用hash凭证匹配器
CustmerMD5Realm realm = new CustmerMD5Realm();
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//设置加密算法
credentialsMatcher.setHashAlgorithmName("md5");
//设置散列次数
credentialsMatcher.setHashIterations(1024);
realm.setCredentialsMatcher(credentialsMatcher);
securityManager.setRealm(realm);
//3、将安全管理器注入安全工具
SecurityUtils.setSecurityManager(securityManager);
//4、通过安全工具类获取subject
Subject subject = SecurityUtils.getSubject();
//5、认证
UsernamePasswordToken token = new UsernamePasswordToken("xiaochen", "123");
try {
subject.login(token);
System.out.println("登陆成功");
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误");
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("密码错误");
}
//认证用户进行授权
if (subject.isAuthenticated()) {
//基于角色权限控制
System.out.println(subject.hasRole("super"));
//基于多角色权限控制
System.out.println(subject.hasAllRoles(Arrays.asList("admin", "user")));
//是否具有一个角色
boolean[] booleans = subject.hasRoles(Arrays.asList("admin", "super", "user"));
for (boolean aBoolean : booleans) {
System.out.println(aBoolean);
}
}
}
}
基于权限字符
修改CustmerMD5Realm
package org.hz52.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
/**
* @Program: shiro_demo
* @Description: 使用自定义realm加入md5+salt+hash
* @Author: 52Hz
* @CreationTime: 2021年10月18日 9:25 星期一
**/
public class CustmerMD5Realm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String primaryPrincipal = (String) principals.getPrimaryPrincipal();
System.out.println("身份信息" + primaryPrincipal);
//根据身份信息 用户名 货当前用户的角色信息 ,以及权限信息 xiaochen admin users
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//将数据库中的查询角色信息赋值给权限对象
simpleAuthorizationInfo.addRole("admin");
simpleAuthorizationInfo.addRole("user");
//将数据库中查询权限信息赋值给权限对象
simpleAuthorizationInfo.addStringPermission("user:*:01");
simpleAuthorizationInfo.addStringPermission("product:create");
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//获取身份信息
String principal = (String) token.getPrincipal();
//根据用户名查询数据库
if ("xiaochen".equals(principal)) {
//参数1:数据库用户名 参数2:数据库MD5+Salt之后的密码 参数3:注册
return new SimpleAuthenticationInfo(principal,
"6abca58de7461f2b5d795d3a6e79aaa6",
ByteSource.Util.bytes("X0#31"),
this.getName());
}
return null;
}
}
修改testCustmerMD5Authenticator
package org.hz52;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.hz52.realm.CustmerMD5Realm;
import java.util.Arrays;
/**
* @Program: shiro_demo
* @Description:
* @Author: 52Hz
* @CreationTime: 2021年10月18日 9:27 星期一
**/
public class testCustmerMD5Authenticator {
public static void main(String[] args) {
//1、创建安全管理器
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//2、注入realm
//设置realm使用hash凭证匹配器
CustmerMD5Realm realm = new CustmerMD5Realm();
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//设置加密算法
credentialsMatcher.setHashAlgorithmName("md5");
//设置散列次数
credentialsMatcher.setHashIterations(1024);
realm.setCredentialsMatcher(credentialsMatcher);
securityManager.setRealm(realm);
//3、将安全管理器注入安全工具
SecurityUtils.setSecurityManager(securityManager);
//4、通过安全工具类获取subject
Subject subject = SecurityUtils.getSubject();
//5、认证
UsernamePasswordToken token = new UsernamePasswordToken("xiaochen", "123");
try {
subject.login(token);
System.out.println("登陆成功");
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误");
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("密码错误");
}
//认证用户进行授权
if (subject.isAuthenticated()) {
//基于角色权限控制
System.out.println(subject.hasRole("super"));
//基于多角色权限控制
System.out.println(subject.hasAllRoles(Arrays.asList("admin", "user")));
//是否具有一个角色
boolean[] booleans = subject.hasRoles(Arrays.asList("admin", "super", "user"));
for (boolean aBoolean : booleans) {
System.out.println(aBoolean);
}
//基于权限字符串的访问控制 资源标识符:操作:资源类型
System.out.println("权限:" + subject.isPermitted("user:update:01"));
System.out.println("权限:" + subject.isPermitted("product:create:02"));
//分别具有哪些权限
boolean[] permitted = subject.isPermitted("user:*:01", "order:*:10");
for (boolean b : permitted) {
System.out.println(b);
}
//同时具有哪些权限
boolean permittedAll = subject.isPermittedAll("user:*:01", "product:create:01");
System.out.println(permittedAll);
}
}
}