Shiro配置
shiro核心配置类
MyShiroRealm
继承 AuthorizingRealm 重写doGetAuthorizationInfo(授权),doGetAuthenticationInfo(鉴权)方法
MyShiroRealm
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
/**
* create by: liumeng
* description: 授权
* create time: 2019/8/29 14:21
* @param : principalCollection
* @return : org.apache.shiro.authz.AuthorizationInfo
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
UserInfoEntity user = (UserInfoEntity) principalCollection.getPrimaryPrincipal();
String userName = user.getUserName();
UserInfoEntity userInfoEntity = userService.getAllUserInfoByUserName(userName);
Set<String> roles = new HashSet<>();
Set<String> perms = new HashSet<>();
for (RoleEntity roleEntity :
userInfoEntity.getRoles()) {
roles.add(roleEntity.getRoleName());
for (MenuEntity menu :
roleEntity.getMenus()) {
perms.add(menu.getPerms());
}
for (OperatorEntity operatorEntity :
roleEntity.getOperators()) {
perms.add(operatorEntity.getPerms());
}
}
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.setRoles(roles);
authorizationInfo.setStringPermissions(perms);
return authorizationInfo;
}
/**
* create by: liumeng
* description: 鉴权
* create time: 2019/8/29 15:11
* @param : authenticationToken
* @return : org.apache.shiro.authc.AuthenticationInfo
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String userName = (String) authenticationToken.getPrincipal();
UserInfoEntity userInfoEntity = userService.getUserInfoByUserName(userName);
if (userInfoEntity == null) {
throw new UnknownAccountException("账号不存在");
}
if (userInfoEntity.getStatus() == 0){
throw new LockedAccountException("账号被锁定");
}
return new SimpleAuthenticationInfo(userInfoEntity, userInfoEntity.getPassword(),
ByteSource.Util.bytes(userInfoEntity.getSalt()), super.getName());
}
userService
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
private UserInfoEntityMapper userMapper;
private RoleEntityMapper roleMapper;
private OperatorEntityMapper operatorMapper;
private MenuEntityMapper menuMapper;
"${shiro.admin}") (
private String adminUser;
public UserInfoEntity getAllUserInfoByUserName(String userName) {
UserInfoEntity userInfoEntity = userMapper.selectAllUserInfoByUserName(userName);
if (userName.equals(adminUser))
{
//超级管理员账号获取所有角色和权限
List<RoleEntity> roleEntities = roleMapper.selectAllRoles();
List<MenuEntity> menuEntities = menuMapper.selectAllMenu();
List<OperatorEntity> operatorEntities = operatorMapper.selectAllOperator();
roleEntities.get(0).setMenus(menuEntities);
roleEntities.get(0).setOperators(operatorEntities);
userInfoEntity.setRoles(roleEntities);
}
return userInfoEntity;
}
public UserInfoEntity getUserInfoByUserName(String userName) {
return userMapper.selectUserInfoByUserName(userName);
}
ShiroConfig
shiro核心配置类
1 |
|