
文章目录
一、RBAC介绍
1.1 RBAC简介
1.2 权限管理
二、 用户认证

三、用户授权

四、权限模型


五、Shiro介绍

六、认证流程

6.1 创建springboot工程并导入依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
6.2 在resources目录下创建shiro.ini文件,IEDA需要安装*.ini并重启方可生效
#对用户的配置
[users]
#对用户的用户名和密码的配置
jack=123
tom=456
6.3 创建认证测试类
package com.qf.authentication;
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.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
public class AuthenticationDemo {
@Test
public void testLoginAndLogout() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jack", "123");
subject.login(token);
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
subject.logout();
isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
}
}
6.4 执行流程
6.5 总结
七、使用realm认证

7.1 创建自定义realm
package com.qf.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 realmDemo extends AuthorizingRealm {
private String realmName = "realmDemo";
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
authenticationToken) throws AuthenticationException {
String principal = (String)authenticationToken.getPrincipal();
System.out.println(principal);
Object credentials = authenticationToken.getCredentials();
String password = new String((char[]) credentials);
System.out.println(password);
if("jack".equals(principal) && "123".equals(password)){
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,password,realmName);
return simpleAuthenticationInfo;
}
return null;
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
}
7.2 在resource目录下创建shiro-realm.ini(注意:realm路径别配置错了)
[main]
#自定义realm
realmDemo=com.qf.realm.realmDemo
#将realm设置到securityManager
securityManager.realms=$realmDemo
7.3 在AuthenticationDemo类中添加方法进行测试
@Test
public void testRealmDemo() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro-realm.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jack", "123");
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
}
八、MD5加密
8.1.创建MD5Demo测试类,进行测试
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
public class MD5Demo {
public static void main(String[] args) {
String password = "123";
String salt = "abcde";
int hashIterations = 2;
Md5Hash md5Hash = new Md5Hash(password, salt, hashIterations);
String password_md5 = md5Hash.toString();
System.out.println(password_md5);
SimpleHash simpleHash = new SimpleHash("md5", password, salt,
hashIterations);
System.out.println(simpleHash.toString());
}
}
8.2在AuthenticationDemo类中添加方法进行测试
@Test
public void testMD5() {
Factory<SecurityManager> factory =
new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jack", "123");
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
}
8.3创建shiro-realm-md5.ini
[main]
#自定义realm
realmDemo=com.qf.realm.realmDemoMD5
#将realm设置到securityManager
securityManager.realms=$realmDemo
8.4创建realmMD5
package com.qf.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.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
public class realmDemoMD5 extends AuthorizingRealm {
private String realmName = "realmDemoMD5";
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String principal = (String)authenticationToken.getPrincipal();
Object credentials = authenticationToken.getCredentials();
String password = new String((char[]) credentials);
System.out.println(password);
Md5Hash md5Hash = new Md5Hash(password, "abcde", 2);
String password_md5 = md5Hash.toString();
System.out.println(password_md5);
String dbpassword = "6585096f3f6735025449cfb351c8cff9";
if("jack".equals(principal) &&password_md5.equals(dbpassword)){
SimpleAuthenticationInfo
simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,password,realmName);
return simpleAuthenticationInfo;
}
return null;
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
}
九、授权流程

9.1创建shiro-permission.ini(shiro-permission.ini里边的内容相当于在数据库)
#用户
[users]
#用户jack的密码是123,此用户具有role1和role2两个角色
jack=123,role1,role2
tom=456,role2
#权限
[roles]
#角色role1对资源user拥有create、update权限
role1=user:create,user:update
#角色role2对资源user拥有create、delete权限
role2=user:create,user:delete
#角色role3对资源user拥有create权限
role3=user:create
9.2创建AuthorizationDemo
package com.qf.authorization;
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.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
public class AuthorizationDemo {
@Test
public void testAuthorization() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro-permission.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jack", "123");
subject.login(token);
boolean authenticated = subject.isAuthenticated();
System.out.println("是否认证成功:"+authenticated);
boolean role1 = subject.hasRole("role1");
System.out.println("role1:"+role1);
boolean allRoles = subject.hasAllRoles(Arrays.asList("role1", "role2"));
System.out.println("role1 + role2:"+allRoles);
boolean permitted = subject.isPermitted("user:create");
System.out.println("user:create:"+permitted);
boolean permittedAll = subject.isPermittedAll("user:create", "user:update");
System.out.println("user:create + user:update:"+permittedAll);
}
}
十、使用realm授权
10.1修改之前realmDemo.java中的doGetAuthorizationInfo方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String primaryPrincipal = (String)principalCollection.getPrimaryPrincipal();
System.out.println("primaryPrincipal:"+primaryPrincipal);
ArrayList<String> roles = new ArrayList<>();
roles.add("role1");
roles.add("role2");
HashSet<String> permissions = new HashSet<>();
permissions.add("user:add");
permissions.add("user:update");
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addStringPermissions(permissions);
return simpleAuthorizationInfo;
}
10.2创建shiro-realm.ini文件,配置自定义的realm,将realm设置到securityManager中,因为之前已经配置过,该步骤可以省略
10.3在AuthorizationDemo中添加方法进行测试
@Test
public void testAuthorization(){
IniSecurityManagerFactory iniSecurityManagerFactory =
new IniSecurityManagerFactory("classpath:shiro-realm.ini");
SecurityManager securityManager = iniSecurityManagerFactory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jack","123");
subject.login(token);
boolean authenticated = subject.isAuthenticated();
System.out.println("是否认证成功:"+authenticated);
boolean role1 = subject.hasRole("role1");
System.out.println("role1:"+role1);
boolean allRoles = subject.hasAllRoles(Arrays.asList("role1", "role2"));
System.out.println("role1 + role2:"+allRoles);
boolean permitted = subject.isPermitted("user:create");
System.out.println("user:create:"+permitted);
boolean permittedAll = subject.isPermittedAll("user:create", "user:update");
System.out.println("user:create + user:update:"+permittedAll);
}
10.4 授权流程
1、对subject进行授权,调用方法isPermitted("permission串")
2、SecurityManager执行授权,通过ModularRealmAuthorizer执行授权
3、ModularRealmAuthorizer执行realm(自定义的Realm)从数据库查询权限数据,调用realm的doGetAuthorizationInfo授权方法
4、realm从数据库查询权限数据,返回ModularRealmAuthorizer
5、ModularRealmAuthorizer调用PermissionResolver进行权限串比对
6、如果比对后,isPermitted中"permission串"在realm查询到权限数据中,说明用户访问permission串有权限,否则 没有权限,抛出异常。