src/main/java/com/ruoyi/framework/security/service/TokenService.java
@@ -1,9 +1,15 @@
package com.ruoyi.framework.security.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.project.system.domain.SysUserDept;
import com.ruoyi.project.system.mapper.SysUserDeptMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,13 +21,15 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.security.LoginUser;
import eu.bitwalker.useragentutils.UserAgent;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.SysRole;
import eu.bitwalker.useragentutils.UserAgent;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.CollectionUtils;
/**
 * token验证处理
@@ -49,7 +57,7 @@
    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;
    private static final Long MILLIS_MINUTE_TWENTY = 20 * 60 * 1000L;
    @Autowired
    private RedisCache redisCache;
@@ -120,24 +128,28 @@
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.LOGIN_USER_KEY, token);
        claims.put(Constants.JWT_USERNAME, loginUser.getUsername());
        return createToken(claims);
    }
    /**
     * 验证令牌有效期,相差不足20分钟,自动刷新缓存
     * 
     * @param token 令牌
     * @param loginUser 登录信息
     * @return 令牌
     */
    public void verifyToken(LoginUser loginUser)
    {
        long expireTime = loginUser.getExpireTime();
        long currentTime = System.currentTimeMillis();
        if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
        if (expireTime - currentTime <= MILLIS_MINUTE_TWENTY)
        {
            refreshToken(loginUser);
        }
    }
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
    /**
     * 刷新令牌有效期
@@ -148,11 +160,83 @@
    {
        loginUser.setLoginTime(System.currentTimeMillis());
        loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
        loginUser.setDeptIds(getDeptIdsByUserId(loginUser.getUserId()));
        if (loginUser.getDeptIds() != null && loginUser.getDeptIds().length > 0)
        {
            loginUser.setCurrentDeptId(loginUser.getDeptIds()[0]);
        }
        loginUser.setDataScope(resolveDataScope(loginUser));
        // 根据uuid将loginUser缓存
        String userKey = getTokenKey(loginUser.getToken());
        redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
    }
    public String resolveDataScope(LoginUser loginUser)
    {
        if (loginUser == null || loginUser.getUser() == null || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
        {
            return null;
        }
        boolean hasCustom = false;
        boolean hasDeptAndChild = false;
        boolean hasDept = false;
        boolean hasSelf = false;
        for (SysRole role : loginUser.getUser().getRoles())
        {
            if (role == null || !"0".equals(role.getStatus()))
            {
                continue;
            }
            if ("1".equals(role.getDataScope()))
            {
                return "1";
            }
            if ("2".equals(role.getDataScope()))
            {
                hasCustom = true;
            }
            else if ("4".equals(role.getDataScope()))
            {
                hasDeptAndChild = true;
            }
            else if ("3".equals(role.getDataScope()))
            {
                hasDept = true;
            }
            else if ("5".equals(role.getDataScope()))
            {
                hasSelf = true;
            }
        }
        if (hasCustom)
        {
            return "2";
        }
        if (hasDeptAndChild)
        {
            return "4";
        }
        if (hasDept)
        {
            return "3";
        }
        if (hasSelf)
        {
            return "5";
        }
        return null;
    }
    public Long[] getDeptIdsByUserId(Long userId){
        LambdaQueryWrapper<SysUserDept> sysUserDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserDeptLambdaQueryWrapper.eq(SysUserDept::getUserId, userId);
        List<SysUserDept> sysUserDept = sysUserDeptMapper.selectList(sysUserDeptLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(sysUserDept)){
            return null;
        }
        return sysUserDept.stream().map(SysUserDept::getDeptId).toArray(Long[]::new);
    }
    /**
     * 设置用户代理信息
     * 
@@ -167,7 +251,7 @@
        loginUser.setBrowser(userAgent.getBrowser().getName());
        loginUser.setOs(userAgent.getOperatingSystem().getName());
    }
    /**
     * 从数据声明生成令牌
     *