kongzy
2024-01-30 1123d12eb51d23edcd4a39660ef8cd47802b931d
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
package com.nanometer.smartlab.realm;
 
import com.nanometer.smartlab.entity.BaseRolePage;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.service.BaseRolePageService;
import com.nanometer.smartlab.service.SysUserService;
import com.nanometer.smartlab.util.Constants;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @description:shiro权限认证
 * @author:zhixuan.wang
 * @date:2015/10/1 14:51
 */
public class ShiroDbRealm extends AuthorizingRealm {
    private static Logger LOGGER = Logger.getLogger(ShiroDbRealm.class);
 
    @Resource
    private SysUserService sysUserService;
    @Resource
    private BaseRolePageService baseRolePageService;
 
    public ShiroDbRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
        super(cacheManager, matcher);
    }
 
    /**
     * Shiro登录认证(原理:用户提交 用户名和密码  --- shiro 封装令牌 ---- realm 通过用户名将密码查询返回 ---- shiro 自动去比较查询出密码和用户输入密码是否一致---- 进行登陆控制 )
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        final String account = String.valueOf(usernamePasswordToken.getUsername());
        SysUser user = this.sysUserService.getSysUserByAccount(account);
 
        if (user == null) {
            throw new UnknownAccountException();
        }
 
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(), getName());
 
        return authenticationInfo;
    }
 
    /**
     * Shiro权限认证
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
 
        String account = (String) super.getAvailablePrincipal(principals);
        List<String> roles = new ArrayList<String>();
        List<String> permissions = new ArrayList<String>();
 
        SysUser user = this.sysUserService.getSysUserByAccount(account);
        if(user != null && user.getRoleId() != null) {
            roles.add(user.getRoleName());
 
            List<BaseRolePage> baseRolePageList = baseRolePageService.getBaseRolePageList(user.getRoleId(), null);
            if (baseRolePageList != null && baseRolePageList.size() > 0) {
                for (BaseRolePage baseRolePage : baseRolePageList) {
                    permissions.add(baseRolePage.getPageId());
                }
            }
        }
 
        info.addRoles(roles);
        info.addStringPermissions(permissions);
        return info;
    }
 
    @Override
    public void onLogout(PrincipalCollection principals) {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.getSession(true).removeAttribute(Constants.SESSION_USER);
        super.onLogout(principals);
    }
 
    /**
     * 清除用户缓存
     * @param loginName
     */
    public void removeUserCache(String loginName){
        Cache<Object, AuthenticationInfo> cache = getAuthenticationCache();
        if (null != cache){
            cache.remove(loginName);
        }
        SimplePrincipalCollection principals = new SimplePrincipalCollection();
        principals.add(loginName, super.getName());
        super.clearCachedAuthenticationInfo(principals);
    }
 
}