| | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | | import com.ruoyi.common.core.exception.GlobalException; |
| | | import com.ruoyi.common.core.exception.user.StudyLoginException; |
| | | import com.ruoyi.common.core.utils.JwtUtils; |
| | | import com.ruoyi.common.core.utils.ServletUtils; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | |
| | | private final static long expireTime = CacheConstants.EXPIRATION; |
| | | |
| | | private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY; |
| | | |
| | | private final static String ACCESS_TOKEN_STUDY = CacheConstants.LOGIN_TOKEN_KEY_STUDY; |
| | | |
| | | private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE; |
| | | |
| | |
| | | public LoginUserParent getLoginUserStudy() { |
| | | LoginUserParent userStudy = getLoginUserStudy(ServletUtils.getRequest()); |
| | | if (null == userStudy) { |
| | | throw new GlobalException("登录失效,请重新登录!"); |
| | | throw new StudyLoginException("登录失效,请重新登录!", 504); |
| | | } |
| | | return userStudy; |
| | | } |
| | |
| | | */ |
| | | public LoginUserParent getLoginUserStudy(String token) { |
| | | LoginUserParent user = null; |
| | | try { |
| | | if (StringUtils.isNotEmpty(token)) { |
| | | String userkey = JwtUtils.getUserKeyStudy(token); |
| | | user = redisService.getCacheObject(getTokenKey(userkey)); |
| | | return user; |
| | | if (StringUtils.isNotEmpty(token)) { |
| | | String userkey = JwtUtils.getUserKeyStudy(token); |
| | | user = redisService.getCacheObject(getTokenKeyStudy(userkey)); |
| | | // 再次判断登录状态是否已过期 |
| | | if (null == user) { |
| | | throw new StudyLoginException("登录信息已过期,请重新登录!", 504); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 优先判断当前账号是否已在其他设备登录 |
| | | if (!user.getIsCanLogin()) { |
| | | throw new StudyLoginException("当前登录账号在其他设备登录!", 505); |
| | | } |
| | | // 再次判断登录状态是否已过期 |
| | | if (System.currentTimeMillis() > user.getExpireTime()) { |
| | | throw new StudyLoginException("登录信息已过期,请重新登录!", 504); |
| | | } |
| | | return user; |
| | | } |
| | | return user; |
| | | } |
| | |
| | | if (StringUtils.isNotEmpty(token)) { |
| | | String userkey = JwtUtils.getUserKey1(token); |
| | | redisService.deleteObject(getTokenKey(userkey)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 学习端删除用户缓存信息 |
| | | */ |
| | | public void delLoginUserStudy(String token) { |
| | | if (StringUtils.isNotEmpty(token)) { |
| | | String userkey = JwtUtils.getUserKeyStudy(token); |
| | | redisService.deleteObject(getTokenKeyStudy(userkey)); |
| | | } |
| | | } |
| | | |
| | |
| | | * 学习端用户登录 |
| | | */ |
| | | public void refreshTokenStudy(LoginUserParent dto) { |
| | | // 获取所有 login_tokens: 前缀的登录缓存 |
| | | Set redisCache = redisService.getKeysPrefix(ACCESS_TOKEN + "*"); |
| | | // 获取所有 login_tokens_study: 前缀的登录缓存 |
| | | Set redisCache = redisService.getKeysPrefix(ACCESS_TOKEN_STUDY); |
| | | for (Object key : redisCache) { |
| | | String strKey = String.valueOf(key); |
| | | // 根据 login_tokens:加密token 获取用户登录信息 |
| | | Object redisCacheUserInfo = redisService.getCacheObject(strKey); |
| | | LoginUserParent redisUserInfo = JSONObject.parseObject(JSONObject.toJSONString(redisCacheUserInfo), LoginUserParent.class); |
| | | // 单点逻辑 |
| | | if (dto.getPhone().equals(redisUserInfo.getPhone())) { |
| | | redisService.deleteObject(strKey); |
| | | // 单点逻辑,如果当前用户已处于登录状态并再次登录,则清除该用户上一次登录token |
| | | if (dto.getUserid().equals(redisUserInfo.getUserid())) { |
| | | // 被挤账户 可登录状态 已经为 false时,跳出循环 |
| | | if (!redisUserInfo.getIsCanLogin()) { |
| | | continue; |
| | | } |
| | | // 设置能否登录字段为 否,当该token登录时,isCanLogin为false表示账号被挤 |
| | | redisUserInfo.setIsCanLogin(Boolean.FALSE); |
| | | redisService.setCacheObject(strKey, redisUserInfo, redisService.getExpire(strKey), TimeUnit.SECONDS); |
| | | } |
| | | } |
| | | // 单点登录逻辑 |
| | | dto.setLoginTime(System.currentTimeMillis()); |
| | | dto.setExpireTime(dto.getLoginTime() + expireTime * MILLIS_MINUTE); |
| | | // 根据uuid将loginUser缓存 |
| | | String userKey = getTokenKey(dto.getToken()); |
| | | String userKey = getTokenKeyStudy(dto.getToken()); |
| | | redisService.setCacheObject(userKey, dto, expireTime, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | private String getTokenKey(String token) { |
| | | return ACCESS_TOKEN + token; |
| | | } |
| | | |
| | | private String getTokenKeyStudy(String token) { |
| | | return ACCESS_TOKEN_STUDY + token; |
| | | } |
| | | |
| | | } |