kongzy
2023-09-22 3124f3a5b7f45d043b228829b6b3a2e541b31574
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
package com.nms.swspkmas_standalone.shiro.realm;
 
import com.nms.swspkmas_standalone.entity.User;
import com.nms.swspkmas_standalone.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
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;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.HashSet;
import java.util.Set;
 
/**
 * @Author ling.quan
 * @Date 2022/2/17 16:45
 * @Desciption 同时开启身份验证和权限验证,需要继承 AuthorizingRealm
 *  * 并实现其  doGetAuthenticationInfo()和 doGetAuthorizationInfo 两个方法
 */
public class ShiroRealm extends AuthorizingRealm {
 
    @Autowired
    private UserService userService;
 
    /**
     * 限定这个 Realm 只处理 UsernamePasswordToken
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }
 
    /**
     * 查询数据库,将获取到的用户安全数据封装返回
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 从 AuthenticationToken 中获取当前用户
        String username = (String) token.getPrincipal();
        // 查询数据库获取用户信息
        User user = userService.getByUserName(username);
 
        // 用户不存在
        if (user == null) {
            throw new UnknownAccountException("用户不存在!");
        }
 
        // 使用用户id作为盐值
        ByteSource credentialsSalt = ByteSource.Util.bytes("nms");
 
        /**
         * 将获取到的用户数据封装成 AuthenticationInfo 对象返回,此处封装为 SimpleAuthenticationInfo 对象。
         *  参数1. 认证的实体信息,可以是从数据库中获取到的用户实体类对象或者用户名
         *  参数2. 查询获取到的登录密码
         *  参数3. 盐值
         *  参数4. 当前 Realm 对象的名称,直接调用父类的 getName() 方法即可
         */
//        return new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt,
//                getName());
 
        return new SimpleAuthenticationInfo(user, user.getPassword(),
                getName());
    }
 
    /**
     * 查询数据库,将获取到的用户的角色及权限信息返回
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // 获取当前用户
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        // UserEntity currentUser = (UserEntity) principals.getPrimaryPrincipal();
        /*if (o == null) {
            UserRole userRole = userRoleService.getByUserId(currentUser.getId());
            RoleEnum roleEnum = RoleEnum.getByCode(userRole.getRoleId());
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(currentUser, userVO);
            if (roleEnum != null) {
                userVO.setRoleName(roleEnum.getName());
                info.setRoles(Sets.newHashSet(roleEnum.getName()));
                //info.setStringPermissions(perms);
            }
            redisUtils.set(redisKey, userVO, 60 * 60 * 24);
        } else {
            UserVO userVO = (UserVO) o;
            info.setRoles(Sets.newHashSet(userVO.getRoleName()));
            //info.setStringPermissions(perms);
        }*/
        Set roleSet=new HashSet();
        roleSet.add("1");
        info.setRoles(roleSet);
        //info.setStringPermissions(perms);
        return info;
    }
 
}