创作来源
视频作者:编程不良人
视频名称:2020最新版Shiro教程,整合SpringBoot项目实战教程
视频链接:https://www.bilibili.com/video/BV1uz4y197Zm
特别声明:学习笔记整理,仅用于个人学习交流使用
一、权限的管理
1.1.什么是权限
基本上只要涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源。
权限管理包括用户身份认证和授权两部分,简称认证授权。对于需要访问控制的资源用户首先经过身份认证,认证通过后用户具有该资源的访问权限方可访问。
1.2.什么是认证
身份认证,就是判断一个用户是否为合法用户的处理过程。最常用的简单身份认证方式是系统通过核对用户输入的用户名和口令,看其是否与系统中存储的该用户的用户名和口令一致,来判断用户身份是否正确。对于采用指纹等系统,则出示指纹;对于硬件Key等刷卡系统,则需要刷卡。
1.3.什么是授权
授权,即访问控制,控制谁可以访问哪些资源。主体进行身份认证后需要分配权限方可访问系统的资源,对于某些资源没有权限是无法访问的。
二、什么是Shiro
Shiro是Apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权、加密、会话管理等功能,组成了一个通用的安全认证框架。
官方网站:https://shiro.apache.org/
参考手册:https://shiro.apache.org/reference.html
Apache Shiro™ is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.
————————————————————
Shiro是一个功能强大且易于使用的Java安全框架,它执行身份验证、授权、加密和会话管理。使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序—从最小的移动应用程序到最大的web和企业应用程序。
三、Shiro的核心架构
3.1 Subject
Subject即主体,外部应用与Subject进行交互,Subject记录了当前操作用户,将用户的概念理解为当前操作的主体,可能是一个通过浏览器请求的用户,也可能是一个运行的程序。
Subject在Shiro中是一个接口,接口中定义了很多认证授相关的方法,外部程序通过Subject进行认证授,而Subject是通过SecurityManager安全管理器进行认证授权
3.2 SecurityManager
SecurityManager即安全管理器,对全部的Subject进行安全管理,它是Shiro的核心,负责对所有的Subject进行安全管理。通过SecurityManager可以完成Subject的认证、授权等,实质上SecurityManager是通过Authenticator进行认证,通过Authorizer进行授权,通过SessionManager进行会话管理等。
SecurityManager是一个接口,继承了Authenticator, Authorizer, SessionManager这三个接口。
3.3 Authenticator
Authenticator即认证器,对用户身份进行认证,Authenticator是一个接口,Shiro提供ModularRealmAuthenticator实现类,通过ModularRealmAuthenticator基本上可以满足大多数需求,也可以自定义认证器。
3.4 Authorizer
Authorizer即授权器,用户通过认证器认证通过,在访问功能时需要通过授权器判断用户是否有此功能的操作权限。
3.5 Realm
Realm即领域,相当于DataSource数据源,SecurityManager进行安全认证需要通过Realm获取用户权限数据,比如:如果用户身份数据在数据库那么Realm就需要从数据库获取用户身份信息。
注意:不要把Realm理解成只是从数据源取数据,在Realm中还有认证授权校验的相关的代码。
3.6 SessionManager
SessionManager即会话管理,Shiro框架定义了一套会话管理,它不依赖web容器的Session,所以Shiro可以使用在非web应用上,也可以将分布式应用的会话集中在一点管理,此特性可使它实现单点登录。
3.7 SessionDAO
SessionDAO即会话dao,是对Session会话操作的一套接口,比如要将Session存储到数据库,可以通过jdbc将会话存储到数据库。
3.8 CacheManager
CacheManager即缓存管理,将用户权限数据存储在缓存,这样可以提高性能。
3.9 Cryptography
Cryptography即密码管理,Shiro提供了一套加密/解密的组件,方便开发。比如提供常用的散列、加/解密等功能。
四、Shiro中的认证
4.1.关键对象
● Subject:主体
访问系统的用户,主体可以是用户、程序等,进行认证的都称为主体;
● Principal:身份信息
是主体(Subject)进行身份认证的标识,标识必须具有唯一性,如用户名、手机号、邮箱地址等,一个主体可以有多个身份,但是必须有一个主身份(Primary Principal)。
● Credential:凭证信息
是只有主体自己知道的安全信息,如密码、证书等。
4.2.认证流程图
4.3.代码实现
1-创建项目并引入依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.5.3</version>
</dependency>
2-引入shiro配置文件
配置文件:名称随意,以 .ini 结尾,放在 resources 目录下。
注意:在实际的项目开发中并不会使用这种方式,这种方法可以用来初学时练手。
[users]
admin=111111
zhangsan=121212
3-测试代码
package com.ctp;
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.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
public class TestAuthenticator {
public static void main(String[] args) {
//1、创建安全管理器
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//2、给安全管理器设置realm
securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
//3、给SecurityManager工具类设置安全管理器
SecurityUtils.setSecurityManager(securityManager);
//4、获取Subject主体信息
Subject subject = SecurityUtils.getSubject();
//5、获取token令牌信息
UsernamePasswordToken token = new UsernamePasswordToken("admin","111111");
//6、通过token认证
try{
System.out.println("认证前状态:" + subject.isAuthenticated());
subject.login(token);
System.out.println("认证后状态:" + subject.isAuthenticated());
}catch (UnknownAccountException e){
System.out.println("认证失败,账号错误!");
}catch (IncorrectCredentialsException e){
System.out.println("认证失败,密码错误!");
}
}
}
常见的异常类型
● DisabledAccountException(帐号被禁用)
● LockedAccountException(帐号被锁定)
● ExcessiveAttemptsException(登录失败次数过多)
● ExpiredCredentialsException(凭证过期)等
4.5.自定义Realm
认证:
1.最终执行用户名校验是在SimpleAccountRealm类的doGetAuthenticationInfo方法中完成用户名校验
2.最终执行密码校验是在AuthenticatingRealm类的assertCredentialsMatch方法中
总结:
AuthenticatingRealm 认证realm doGetAuthenticationInfo
AuthorizingRealm 授权realm doGetAuthorizationInfo
自定义Realm的作用:放弃使用.ini文件,使用数据库查询。
上边的程序使用的是Shiro自带的IniRealm,IniRealm是从ini配置文件中读取用户的信息,但是实际情况下需要从系统的数据库中读取用户信息,所以需要自定义Realm。
1.Shiro提供的Realm家族
2.Shiro默认使用的Realm
SimpleAccountRealm的部分源码中有两个方法一个是认证(doGetAuthenticationInfo,来自于父类AuthenticatingRealm),一个是授权(doGetAuthorizationInfo,来自于父类AuthorizingRealm)。
自定义Realm继承SimpleAccountRealm的父类AuthorizingRealm即可重写doGetAuthorizationInfo授权方法,又因为AuthorizingRealm继承了AuthenticatingRealm,即可重写doGetAuthenticationInfo认证方法。
源码部分:
public class SimpleAccountRealm extends AuthorizingRealm {
//.......省略部分
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
SimpleAccount account = getUser(upToken.getUsername());
if (account != null) {
if (account.isLocked()) {
throw new LockedAccountException("Account [" + account + "] is locked.");
}
if (account.isCredentialsExpired()) {
String msg = "The credentials for account [" + account + "] are expired";
throw new ExpiredCredentialsException(msg);
}
}
return account;
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = getUsername(principals);
USERS_LOCK.readLock().lock();
try {
return this.users.get(username);
} finally {
USERS_LOCK.readLock().unlock();
}
}
//.......省略部分
}
3.自定义Realm实现
package com.ctp.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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
public class CustomRealm extends AuthorizingRealm {
//自定义授权Realm
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
//自定义认证Realm
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1、从token中获取用户名
String principal = (String) authenticationToken.getPrincipal();
System.out.println(principal);
//2、实际开发中应当根据身份信息使用jdbc mybatis查询相关数据库
//在这里只做简单的演示
//假设username,password是从数据库获得的信息
String username = "admin";
String password = "111111";
//3、进行认证
if(username.equals(principal)){
//参数1:返回数据库中正确的用户名
//参数2:返回数据库中正确密码
//参数3:提供当前realm的名字 this.getName();
return new SimpleAuthenticationInfo(username, password, this.getName());
}
return null;
}
}
4.使用自定义Realm认证实现
package com.ctp;
import com.ctp.realm.CustomRealm;
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.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
public class TestCustomRealmAuthenticator {
public static void main(String[] args) {
//1、创建安全管理对象 securityManager
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//2、给安全管理器设置realm(设置为自定义realm获取认证数据)
defaultSecurityManager.setRealm(new CustomRealm());
//3、给安全工具类中设置默认安全管理器
SecurityUtils.setSecurityManager(defaultSecurityManager);
//4.获取主体对象subject
Subject subject = SecurityUtils.getSubject();
//5.创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("admin","111111");
//6、用户登录
try {
subject.login(token);
if(subject.isAuthenticated()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误!!");
}catch (IncorrectCredentialsException e){
e.printStackTrace();
System.out.println("密码错误!!!");
}
}
}
4.6.MD5+Salt+Hash
MD5信息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。
作用:一般用来加密或者签名(校验和)
特点:MD5算法不可逆如果内容相同无论执行多少次MD5生成结果始终是一致的
网络上提供的MD5在线解密一般是用穷举的方法
生成结果:始终是一个16进制32位长度字符串
1.MD5的基本使用
package com.ctp;
import org.apache.shiro.crypto.hash.Md5Hash;
public class TestShiroMD5 {
public static void main(String[] args) {
//1、MD5
Md5Hash md5Hash1 = new Md5Hash("121212");
System.out.println(md5Hash1.toHex());
//2、MD5 + salt
Md5Hash md5Hash2 = new Md5Hash("121212", "QE@#123");
System.out.println(md5Hash2.toHex());
//3、MD5 + salt + hash
Md5Hash md5Hash3 = new Md5Hash("121212", "QE@#123", 1024);
System.out.println(md5Hash3);
}
}
输出结果:
93279e3308bdbbeed946fc965017f67a
d6757ac0ce74f84a22e53336fc7c4eff
01738dca41fa644059b7a27ef76b4b6d
2.自定义md5+salt的Realm
package com.ctp.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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
public class CustomMD5Realm extends AuthorizingRealm {
//授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
//认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1、从token中获取用户名
String principal = (String) authenticationToken.getPrincipal();
//2、实际开发中应当根据身份信息使用jdbc mybatis查询相关数据库
//在这里只做简单的演示
//假设username,password,salt是从数据库获得的信息
String username = "admin";
String password = "01738dca41fa644059b7a27ef76b4b6d";
String salt = "QE@#123";
//3、进行认证
if (username.equals(principal)) {
//参数1:返回数据库中正确的用户名
//参数2:返回数据库中正确密码
//参数3:返回数据库中正确随机盐
//参数3:提供当前realm的名字 this.getName();
return new SimpleAuthenticationInfo(username, password, ByteSource.Util.bytes(salt), this.getName());
}
return null;
}
}
3.使用md5+salt认证实现
3.1源码分析
通过源码分析得知,Shiro默认在AuthenticatingRealm中指定了密码匹配器,并通过doCredentialsMatch方法实现密码校验。而doCredentialsMatch是在SimpleCredentialsMatcher中实现的,通过源码可以得知Shiro默认的密码匹配方式是通过equals方法进行校验的。
AuthenticatingRealm.java
public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
//……省略
protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
CredentialsMatcher cm = this.getCredentialsMatcher();
if (cm != null) {
if (!cm.doCredentialsMatch(token, info)) {
String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
throw new IncorrectCredentialsException(msg);
}
} else {
throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify credentials during authentication. If you do not wish for credentials to be examined, you can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
}
}
//……省略
}
SimpleCredentialsMatcher.java
public class SimpleCredentialsMatcher extends CodecSupport implements CredentialsMatcher {
//……省略
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
Object tokenCredentials = this.getCredentials(token);
Object accountCredentials = this.getCredentials(info);
return this.equals(tokenCredentials, accountCredentials);
}
//……省略
}
3.2指定匹配器
通过CredentialsMatcher接口的继承图得知符合我们MD5+salt匹配方式的实现类为HashedCredentialsMatcher,所以在我们的自定义Realm中需要通过set方法重新指定密码匹配器为HashedCredentialsMatcher,并通过setHashAlgorithmName方式指定hash算法,通过setHashIterations指定散列次数。
TestCustomMD5RealmAuthenticator.java
package com.ctp;
import com.ctp.realm.CustomMD5Realm;
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;
public class TestCustomMD5RealmAuthenticator {
public static void main(String[] args) {
//1、创建安全管理对象
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//2、设置Realm
CustomMD5Realm md5Realm = new CustomMD5Realm();
//3、设置密码匹配器
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//设置加密方式
matcher.setHashAlgorithmName("MD5");
//设置散列次数
matcher.setHashIterations(1024);
md5Realm.setCredentialsMatcher(matcher);
defaultSecurityManager.setRealm(md5Realm);
//4、给安全工具类中设置默认安全管理器
SecurityUtils.setSecurityManager(defaultSecurityManager);
//5.获取主体对象subject
Subject subject = SecurityUtils.getSubject();
//6.创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("admin", "121212");
//7、用户登录
try {
subject.login(token);
if (subject.isAuthenticated()) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!");
}
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误!!");
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("密码错误!!!");
}
}
}
五、Shiro中的授权
5.1.关键对象
授权可简单理解为who对what(which)进行How操作:
Who,即主体(Subject),主体需要访问系统中的资源。
What,即资源(Resource),如系统菜单、页面、按钮、类方法、系统商品信息等。资源包括资源类型和资源实例,比如商品信息为资源类型,类型为t01的商品为资源实例,编号为001的商品信息也属于资源实例。
How,权限/许可(Permission),规定了主体对资源的操作许可,权限离开资源没有意义,如用户查询权限、用户添加权限、某个类方法的调用权限、编号为001用户的修改权限等,通过权限可知主体对哪些资源都有哪些操作许可。
5.2.授权流程图
5.3.授权方式
- 基于角色的访问控制
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:*")){ //资源类型
//对 所有的资源 用户具有更新的权限
}
5.4.权限字符串
权限字符串的规则是:资源标识符:操作:资源实例标识符,意思是对哪个资源的哪个实例具有什么操作,“:”是资源/操作/实例的分割符,权限字符串也可以使用*通配符。
例子:
用户创建权限:user:create,或user:create:*
用户修改实例001的权限:user:update:001
用户实例001的所有权限:user:*:001
5.5.权限实现方式
- 编程式
Subject subject = SecurityUtils.getSubject();
if(subject.hasRole(“admin”)) {
//有权限
} else {
//无权限
}
- 注解式
@RequiresRoles("admin")
public void hello() {
//有权限
}
- 标签式
JSP/GSP 标签:在JSP/GSP 页面通过相应的标签完成:
<shiro:hasRole name="admin">
<!— 有权限—>
</shiro:hasRole>
注意: Thymeleaf 中使用shiro需要额外集成!
5.6.代码实现
1.自定义Realm实现
package com.ctp.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;
public class CustomMD5Realm extends AuthorizingRealm {
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
Object primaryPrincipal = principalCollection.getPrimaryPrincipal();
System.out.println("当前登录用户:" + primaryPrincipal);
//根据身份信息 用户名 获取当前用户的角色信息,以及权限信息
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//假设 admin,user 是从数据库查到的 角色信息
simpleAuthorizationInfo.addRole("admin");
simpleAuthorizationInfo.addRole("user");
//假设 user:add:* ,user:update:* 是从数据库查到的 权限信息赋值给权限对象
simpleAuthorizationInfo.addStringPermission("user:add:*");
simpleAuthorizationInfo.addStringPermission("user:update:*");
//simpleAuthorizationInfo.addStringPermission("user:*");//user:*:* 可以写成 user:*
return simpleAuthorizationInfo;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1、从token中获取用户名
String principal = (String) authenticationToken.getPrincipal();
//2、实际开发中应当根据身份信息使用jdbc mybatis查询相关数据库
//在这里只做简单的演示
//假设username,password,salt是从数据库获得的信息
String username = "admin";
String password = "01738dca41fa644059b7a27ef76b4b6d";
String salt = "QE@#123";
//3、进行认证
if (username.equals(principal)) {
//参数1:返回数据库中正确的用户名
//参数2:返回数据库中正确密码
//参数3:返回数据库中正确随机盐
//参数3:提供当前realm的名字 this.getName();
return new SimpleAuthenticationInfo(principal, password, ByteSource.Util.bytes(salt), this.getName());
}
return null;
}
}
2.授权实现
package com.ctp;
import com.ctp.realm.CustomMD5Realm;
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 java.util.Arrays;
public class TestCustomMD5RealmAuthenticator {
public static void main(String[] args) {
//1、创建安全管理对象
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//2、设置Realm
CustomMD5Realm md5Realm = new CustomMD5Realm();
//3、设置密码匹配器
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//设置加密方式
matcher.setHashAlgorithmName("MD5");
//设置散列次数
matcher.setHashIterations(1024);
md5Realm.setCredentialsMatcher(matcher);
defaultSecurityManager.setRealm(md5Realm);
//4、给安全工具类中设置默认安全管理器
SecurityUtils.setSecurityManager(defaultSecurityManager);
//5.获取主体对象subject
Subject subject = SecurityUtils.getSubject();
//6.创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("admin", "121212");
//7、用户登录
try {
subject.login(token);
if (subject.isAuthenticated()) {
System.out.println("登录成功!");
} else {
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("admin"));//true
//基于多角色的权限控制
System.out.println(subject.hasAllRoles(Arrays.asList("admin", "user")));//true
System.out.println(subject.hasAllRoles(Arrays.asList("admin", "manager")));//false
//是否具有其中一个角色
boolean[] booleans = subject.hasRoles(Arrays.asList("admin", "user", "manager"));
for (boolean aBoolean : booleans) {
System.out.println(aBoolean);//true//true//false
}
System.out.println("====这是一个分隔符====");
//基于权限字符串的访问控制 资源标识符:操作:资源类型
//用户具有的权限 user:add:* user:update:*
System.out.println("权限:" + subject.isPermitted("user:add:01"));//true
System.out.println("权限:" + subject.isPermitted("user:update:02"));//true
//分别具有哪些权限
boolean[] permitted = subject.isPermitted("user:*:01", "user:update:02");
for (boolean b : permitted) {
System.out.println(b);//false//true
}
//同时具有哪些权限
boolean permittedAll = subject.isPermittedAll("user:update:02", "user:update:03");
System.out.println(permittedAll);//true
}
}
}
文章最后
如果学习过程中总结有不足或有纰漏的地方,还望大家指出以求共同进步,谢谢!