| | |
| | | package com.ruoyi.common.security.service; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import com.ruoyi.system.api.model.LoginUserParent; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | | import com.ruoyi.common.core.utils.JwtUtils; |
| | |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.system.api.model.LoginUser; |
| | | import com.ruoyi.system.api.model.LoginUserParent; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * token验证处理 |
| | |
| | | rspMap.put("expires_in", expireTime); |
| | | return rspMap; |
| | | } |
| | | |
| | | public Map<String, Object> createTokenStudy(LoginUserParent loginUser) |
| | | { |
| | | String token = IdUtils.fastUUID(); |
| | | Integer userId = loginUser.getUserid(); |
| | | String name = loginUser.getName(); |
| | | loginUser.setToken(token); |
| | | loginUser.setIpaddr(IpUtils.getIpAddr()); |
| | | refreshTokenStudy(loginUser); |
| | | // Jwt存储信息 |
| | | Map<String, Object> claimsMap = new HashMap<String, Object>(8); |
| | | claimsMap.put(SecurityConstants.USER_STUDY_KEY, token); |
| | | claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId); |
| | | claimsMap.put(SecurityConstants.DETAILS_USERNAME, name); |
| | | // 接口返回信息 |
| | | Map<String, Object> rspMap = new HashMap<String, Object>(); |
| | | rspMap.put("access_token", JwtUtils.createToken(claimsMap)); |
| | | rspMap.put("expires_in", expireTime); |
| | | return rspMap; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户身份信息 |
| | | * |
| | |
| | | } |
| | | |
| | | /** |
| | | * 学习端获取用户身份信息 |
| | | * |
| | | * @return 用户信息 |
| | | */ |
| | | public LoginUserParent getLoginUserStudy() |
| | | { |
| | | return getLoginUserStudy(ServletUtils.getRequest()); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户身份信息 |
| | | * |
| | | * @return 用户信息 |
| | |
| | | // 获取请求携带的令牌 |
| | | String token = SecurityUtils.getToken(request); |
| | | return getLoginUser1(token); |
| | | } |
| | | |
| | | /** |
| | | * 学习端获取用户身份信息 |
| | | * |
| | | * @return 用户信息 |
| | | */ |
| | | public LoginUserParent getLoginUserStudy(HttpServletRequest request) |
| | | { |
| | | // 获取请求携带的令牌 |
| | | String token = SecurityUtils.getToken(request); |
| | | return getLoginUserStudy(token); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (StringUtils.isNotEmpty(token)) |
| | | { |
| | | String userkey = JwtUtils.getUserKey1(token); |
| | | user = redisService.getCacheObject(getTokenKey(userkey)); |
| | | return user; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | e.printStackTrace(); |
| | | } |
| | | return user; |
| | | } |
| | | |
| | | /** |
| | | * 学习端 获取用户身份信息 |
| | | * |
| | | * @return 用户信息 |
| | | */ |
| | | 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; |
| | | } |
| | |
| | | redisService.setCacheObject(userKey, dto, expireTime, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | /** |
| | | * 学习端用户登录 |
| | | */ |
| | | public void refreshTokenStudy(LoginUserParent dto) |
| | | { |
| | | dto.setLoginTime(System.currentTimeMillis()); |
| | | dto.setExpireTime(dto.getLoginTime() + expireTime * MILLIS_MINUTE); |
| | | // 根据uuid将loginUser缓存 |
| | | String userKey = getTokenKey(dto.getToken()); |
| | | redisService.setCacheObject(userKey, dto, expireTime, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | private String getTokenKey(String token) |
| | | { |
| | | return ACCESS_TOKEN + token; |