自定义Realm

新建一个MyRealm类,继承Shiro框架的AuthorizingRealm类,并实现默认的两个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.ledao.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 java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* 自定义Realm实现认证和授权
*
* @author LeDao
* @company
* @create 2021-09-15 19:49
*/
public class MyRealm extends AuthorizingRealm {

/**
* 授权
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String userName = (String) principalCollection.getPrimaryPrincipal();
//获取角色
Set<String> roles = getRolesByUserName(userName);
//获取权限
Set<String> permissions = getPermissionsByUserName(userName);

SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roles);
simpleAuthorizationInfo.setStringPermissions(permissions);
return simpleAuthorizationInfo;
}

/**
* 认证
*
* @param authenticationToken 从主体传过来的认证信息
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//从主体传过来的认证信息中获得用户名
String userName = (String) authenticationToken.getPrincipal();
//根据用户名获取密码
String password = getPasswordByUserName(userName);
if (password == null) {
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, password, "myRealm");
return simpleAuthenticationInfo;
}

/**
* 存储用户的用户名和密码,用于模拟数据库
*/
Map<String, String> userMap = new HashMap<>(16);

{
//加入一个用户名为ledao,密码为123456的用户
userMap.put("ledao", "123456");
//设置自定义Realm的名称,取什么无所谓
super.setName("myRealm");
}

/**
* 模拟从数据库根据用户名获取密码
*
* @param userName 用户名
* @return
*/
private String getPasswordByUserName(String userName) {
return userMap.get(userName);
}

/**
* 模拟从数据库中根据用户名获取角色数据
*
* @param userName 用户名
* @return
*/
private Set<String> getRolesByUserName(String userName) {
Set<String> roles = new HashSet<>();
roles.add("admin");
roles.add("teacher");
return roles;
}

/**
* 拟从数据库中根据用户名获取权限数据
*
* @param userName 用户名
* @return
*/
private Set<String> getPermissionsByUserName(String userName) {
Set<String> permissions = new HashSet<>();
permissions.add("添加学生");
permissions.add("添加教师");
return permissions;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.ledao;

import com.ledao.realm.MyRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
* @author LeDao
* @company
* @create 2021-09-14 11:10
*/
public class Test {

public static void main(String[] args) {
MyRealm myRealm = new MyRealm();
//构建securityManager环境
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//securityManager环境添加自定义Realm
securityManager.setRealm(myRealm);
//提交认证请求
SecurityUtils.setSecurityManager(securityManager);
//获取当前securityManager环境的subject主体
Subject subject = SecurityUtils.getSubject();
//设置登录用户的token
UsernamePasswordToken token = new UsernamePasswordToken("ledao", "123456");
try {
//登录
subject.login(token);
System.out.println(subject.isAuthenticated() ? "登录成功" : "登录失败");
try {
//检查成功登录的用户是否有admin和teacher两个角色
subject.checkRoles("admin", "teacher");
try {
//检查成功登录的用户是否有admin和teacher1两个角色
subject.checkPermissions("添加学生", "添加教师");
} catch (AuthorizationException e) {
e.printStackTrace();
System.out.println("登录的用户与检查的权限不相符!!");
}
} catch (AuthorizationException e) {
e.printStackTrace();
System.out.println("登录的用户与检查的角色不相符!!");
}
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("用户名或密码错误!!");
}
}
}