MyShiroRealm.java 7.09 KB
Newer Older
liqin's avatar
liqin committed
1 2
package cn.wisenergy.chnmuseum.party.auth.realm;

liqin's avatar
liqin committed
3
import cn.wisenergy.chnmuseum.party.auth.token.JwtToken;
liqin's avatar
liqin committed
4 5 6 7 8
import cn.wisenergy.chnmuseum.party.auth.util.JwtTokenUtil;
import cn.wisenergy.chnmuseum.party.model.*;
import cn.wisenergy.chnmuseum.party.service.PermissionService;
import cn.wisenergy.chnmuseum.party.service.RolePermissionService;
import cn.wisenergy.chnmuseum.party.service.RoleService;
wzp's avatar
wzp committed
9
import cn.wisenergy.chnmuseum.party.service.TUserService;
liqin's avatar
liqin committed
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
import cn.wisenergy.chnmuseum.party.service.impl.EmployeeRoleServiceImpl;
import cn.wisenergy.chnmuseum.party.service.impl.EmployeeServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.apache.shiro.subject.SimplePrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * 身份校验核心类
 */
public class MyShiroRealm extends AuthorizingRealm {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyShiroRealm.class);

liqin's avatar
liqin committed
36
    private static final String SHIRO_JWT_TOKEN = "shiro:jwt:token:";
liqin's avatar
liqin committed
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

    //用户登录次数计数  redisKey 前缀
    private String SHIRO_LOGIN_COUNT = "shiro_login_count_";

    //用户登录是否被锁定    一小时 redisKey 前缀
    private String SHIRO_IS_LOCK = "shiro_is_lock_";

    @Resource
    private PermissionService permissionService;

    @Resource
    private RoleService roleService;

    @Resource
    private EmployeeServiceImpl employeeService;

    @Resource
    private EmployeeRoleServiceImpl employeeRoleService;

    @Resource
    private RolePermissionService rolePermissionService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

wzp's avatar
wzp committed
62 63 64
    @Resource
    private TUserService userService;

liqin's avatar
liqin committed
65 66 67 68 69 70 71 72 73 74
    /**
     * 必须重写此方法,不然Shiro会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 认证信息.(身份验证) : Authentication 是用来验证用户身份
liqin's avatar
liqin committed
75
     * @throws AuthenticationException
liqin's avatar
liqin committed
76 77
     */
    @Override
liqin's avatar
liqin committed
78 79 80 81 82
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String credentials = (String) token.getCredentials();
        if (credentials == null) {
            throw new AuthenticationException("token为空!");
        }
liqin's avatar
liqin committed
83
        Boolean hasToken = stringRedisTemplate.hasKey(SHIRO_JWT_TOKEN + credentials);
liqin's avatar
liqin committed
84 85 86 87
        if (hasToken == null || !hasToken) {
            throw new AuthenticationException("用户未登录!");
        }

liqin's avatar
liqin committed
88
        String username = JwtTokenUtil.getUsername(credentials);
liqin's avatar
liqin committed
89 90 91 92 93 94 95
        if (username == null) {
            throw new AuthenticationException("token invalid");
        }
        LOGGER.info("MyShiroRealm doGetAuthenticationInfo().username=" + username);

        // 通过username从数据库中查找
        // 实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法
wzp's avatar
wzp committed
96 97 98
        String userId = JwtTokenUtil.getEmployeeId(credentials);
        TUser user = userService.getById(userId);
        if (user == null) {
liqin's avatar
liqin committed
99 100 101
            throw new AuthenticationException("User does not exist!");
        }

liqin's avatar
liqin committed
102
        if (JwtTokenUtil.verify(credentials, username) == null) {
liqin's avatar
liqin committed
103 104 105
            throw new AuthenticationException("token invalid");
        }

wzp's avatar
wzp committed
106
        return new SimpleAuthenticationInfo(new TUser(user.getId(), credentials), credentials, getName());
liqin's avatar
liqin committed
107 108 109 110 111 112 113 114
    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("权限认证方法:MyShiroRealm.doGetAuthorizationInfo()");
wzp's avatar
wzp committed
115 116
        TUser user = (TUser) principals.getPrimaryPrincipal();
        Boolean hasToken = stringRedisTemplate.hasKey(SHIRO_JWT_TOKEN + user.getJwtToken());
liqin's avatar
liqin committed
117 118 119
        if (hasToken == null || !hasToken) {
            throw new AuthenticationException("token invalid!");
        }
liqin's avatar
liqin committed
120

wzp's avatar
wzp committed
121
        String userId = JwtTokenUtil.getEmployeeId(user.getJwtToken());
liqin's avatar
liqin committed
122 123

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
wzp's avatar
wzp committed
124 125 126 127 128
        List<Role> list = roleService.selectRoleByUserId(userId);
//        // 根据用户ID查询角色(role),放入到Authorization里。
//        Map<String, Object> map = new HashMap<>();
//        map.put("user_id", userId);
//        List<EmployeeRole> employeeRoleList = this.employeeRoleService.listByMap(map);
liqin's avatar
liqin committed
129
        List<String> ridList = new LinkedList<>();
wzp's avatar
wzp committed
130 131 132 133
//        for (EmployeeRole employeeRole : employeeRoleList) {
//            ridList.add(employeeRole.getRoleId());
//        }
//        List<Role> roleList = this.roleService.listByIds(ridList);
liqin's avatar
liqin committed
134
        Set<String> roleSet = new HashSet<>();
wzp's avatar
wzp committed
135
        for (Role role : list) {
liqin's avatar
liqin committed
136
            roleSet.add(role.getAlias());
wzp's avatar
wzp committed
137
            ridList.add(role.getId());
liqin's avatar
liqin committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        }
        info.setRoles(roleSet);

        // 根据用户ID查询权限(permission)放入到Authorization里。
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.in("rid", ridList).select("pid");
        List<Object> permissionIdList = this.rolePermissionService.listObjs(wrapper);
        List<Permission> permissionList = new ArrayList<>();
        if (permissionIdList.size() > 0) {
            QueryWrapper<Permission> ew = new QueryWrapper<>();
            ew.in("id", permissionIdList);
            permissionList = this.permissionService.list(ew);
        }
        Set<String> permissionSet = new HashSet<>();
        for (Permission permission : permissionList) {
            permissionSet.add(permission.getUrl());
        }
        info.setStringPermissions(permissionSet);
        return info;
    }

    public void clearCachedAuthenticationInfo(String token) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(new Employee(token), getName());
        clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    private void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    private void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}