|  | @@ -7,13 +7,14 @@ import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  |  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.Constants;
 | 
	
		
			
				|  |  | +import com.ruoyi.common.core.constant.SecurityConstants;
 | 
	
		
			
				|  |  |  import com.ruoyi.common.core.utils.IdUtils;
 | 
	
		
			
				|  |  | -import com.ruoyi.common.core.utils.SecurityUtils;
 | 
	
		
			
				|  |  | +import com.ruoyi.common.core.utils.JwtUtils;
 | 
	
		
			
				|  |  |  import com.ruoyi.common.core.utils.ServletUtils;
 | 
	
		
			
				|  |  |  import com.ruoyi.common.core.utils.StringUtils;
 | 
	
		
			
				|  |  |  import com.ruoyi.common.core.utils.ip.IpUtils;
 | 
	
		
			
				|  |  |  import com.ruoyi.common.redis.service.RedisService;
 | 
	
		
			
				|  |  | +import com.ruoyi.common.security.utils.SecurityUtils;
 | 
	
		
			
				|  |  |  import com.ruoyi.system.api.model.LoginUser;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -27,31 +28,41 @@ public class TokenService
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private RedisService redisService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
 | 
	
		
			
				|  |  | +    protected static final long MILLIS_SECOND = 1000;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private final static long expireTime = CacheConstants.EXPIRATION;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    protected static final long MILLIS_SECOND = 1000;
 | 
	
		
			
				|  |  | +    private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 创建令牌
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public Map<String, Object> createToken(LoginUser loginUser)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        // 生成token
 | 
	
		
			
				|  |  |          String token = IdUtils.fastUUID();
 | 
	
		
			
				|  |  | +        Long userId = loginUser.getSysUser().getUserId();
 | 
	
		
			
				|  |  | +        String userName = loginUser.getSysUser().getUserName();
 | 
	
		
			
				|  |  |          loginUser.setToken(token);
 | 
	
		
			
				|  |  | -        loginUser.setUserid(loginUser.getSysUser().getUserId());
 | 
	
		
			
				|  |  | -        loginUser.setUsername(loginUser.getSysUser().getUserName());
 | 
	
		
			
				|  |  | +        loginUser.setUserid(userId);
 | 
	
		
			
				|  |  | +        loginUser.setUsername(userName);
 | 
	
		
			
				|  |  |          loginUser.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
 | 
	
		
			
				|  |  |          refreshToken(loginUser);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // 保存或更新用户token
 | 
	
		
			
				|  |  | -        Map<String, Object> map = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | -        map.put("access_token", token);
 | 
	
		
			
				|  |  | -        map.put("expires_in", EXPIRE_TIME);
 | 
	
		
			
				|  |  | -        redisService.setCacheObject(ACCESS_TOKEN + token, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
 | 
	
		
			
				|  |  | -        return map;
 | 
	
		
			
				|  |  | +        // Jwt存储信息
 | 
	
		
			
				|  |  | +        Map<String, Object> claimsMap = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | +        claimsMap.put(SecurityConstants.USER_KEY, token);
 | 
	
		
			
				|  |  | +        claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
 | 
	
		
			
				|  |  | +        claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 接口返回信息
 | 
	
		
			
				|  |  | +        Map<String, Object> rspMap = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | +        rspMap.put("access_token", JwtUtils.createToken(claimsMap));
 | 
	
		
			
				|  |  | +        rspMap.put("expires_in", expireTime);
 | 
	
		
			
				|  |  | +        return rspMap;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
	
		
			
				|  | @@ -83,13 +94,20 @@ public class TokenService
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public LoginUser getLoginUser(String token)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        if (StringUtils.isNotEmpty(token))
 | 
	
		
			
				|  |  | +        LoginUser user = null;
 | 
	
		
			
				|  |  | +        try
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (StringUtils.isNotEmpty(token))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                String userkey = JwtUtils.getUserKey(token);
 | 
	
		
			
				|  |  | +                user = redisService.getCacheObject(getTokenKey(userkey));
 | 
	
		
			
				|  |  | +                return user;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        catch (Exception e)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            String userKey = getTokenKey(token);
 | 
	
		
			
				|  |  | -            LoginUser user = redisService.getCacheObject(userKey);
 | 
	
		
			
				|  |  | -            return user;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +        return user;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
	
		
			
				|  | @@ -103,12 +121,30 @@ public class TokenService
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 删除用户缓存信息
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  |      public void delLoginUser(String token)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          if (StringUtils.isNotEmpty(token))
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            String userKey = getTokenKey(token);
 | 
	
		
			
				|  |  | -            redisService.deleteObject(userKey);
 | 
	
		
			
				|  |  | +            String userkey = JwtUtils.getUserKey(token);
 | 
	
		
			
				|  |  | +            redisService.deleteObject(getTokenKey(userkey));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 验证令牌有效期,相差不足120分钟,自动刷新缓存
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param loginUser
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void verifyToken(LoginUser loginUser)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        long expireTime = loginUser.getExpireTime();
 | 
	
		
			
				|  |  | +        long currentTime = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +        if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            refreshToken(loginUser);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -120,10 +156,10 @@ public class TokenService
 | 
	
		
			
				|  |  |      public void refreshToken(LoginUser loginUser)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          loginUser.setLoginTime(System.currentTimeMillis());
 | 
	
		
			
				|  |  | -        loginUser.setExpireTime(loginUser.getLoginTime() + EXPIRE_TIME * MILLIS_SECOND);
 | 
	
		
			
				|  |  | +        loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
 | 
	
		
			
				|  |  |          // 根据uuid将loginUser缓存
 | 
	
		
			
				|  |  |          String userKey = getTokenKey(loginUser.getToken());
 | 
	
		
			
				|  |  | -        redisService.setCacheObject(userKey, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
 | 
	
		
			
				|  |  | +        redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private String getTokenKey(String token)
 |