| | |
| | | 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;
|
| | | import org.springframework.beans.factory.annotation.Value;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.ruoyi.common.constant.CacheConstants;
|
| | | import com.ruoyi.common.constant.Constants;
|
| | | import com.ruoyi.common.utils.ServletUtils;
|
| | |
| | | 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 com.ruoyi.project.system.domain.SysUserDept;
|
| | | import com.ruoyi.project.system.mapper.SysUserDeptMapper;
|
| | | import eu.bitwalker.useragentutils.UserAgent;
|
| | | import io.jsonwebtoken.Claims;
|
| | | import io.jsonwebtoken.Jwts;
|
| | | import io.jsonwebtoken.SignatureAlgorithm;
|
| | | import io.jsonwebtoken.security.Keys;
|
| | | import jakarta.servlet.http.HttpServletRequest;
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.beans.factory.annotation.Value;
|
| | | import org.springframework.stereotype.Component;
|
| | | import org.springframework.util.CollectionUtils;
|
| | |
|
| | | import javax.crypto.SecretKey;
|
| | | import java.nio.charset.StandardCharsets;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.concurrent.TimeUnit;
|
| | |
|
| | | /**
|
| | | * token验证处理
|
| | |
| | | loginUser.setLoginTime(System.currentTimeMillis());
|
| | | loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
| | | loginUser.setDeptIds(getDeptIdsByUserId(loginUser.getUserId()));
|
| | | loginUser.setCurrentDeptId(loginUser.getDeptIds()[0]);
|
| | | 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){
|
| | |
| | | loginUser.setOs(userAgent.getOperatingSystem().getName());
|
| | | }
|
| | |
|
| | | private SecretKey getSigningKey() {
|
| | | byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
| | | return Keys.hmacShaKeyFor(keyBytes);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 从数据声明生成令牌
|
| | | *
|
| | |
| | | */
|
| | | private String createToken(Map<String, Object> claims)
|
| | | {
|
| | | String token = Jwts.builder()
|
| | | .setClaims(claims)
|
| | | .signWith(SignatureAlgorithm.HS512, secret).compact();
|
| | | return token;
|
| | | return Jwts.builder()
|
| | | .claims(claims) // 注意:新版方法名变了,不再是 setClaims
|
| | | .signWith(getSigningKey(), Jwts.SIG.HS512) // 使用新的签名常量
|
| | | .compact();
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | private Claims parseToken(String token)
|
| | | {
|
| | | return Jwts.parser()
|
| | | .setSigningKey(secret)
|
| | | .parseClaimsJws(token)
|
| | | .getBody();
|
| | | .verifyWith(getSigningKey()) // 新版使用 verifyWith
|
| | | .build()
|
| | | .parseSignedClaims(token)
|
| | | .getPayload();
|
| | | }
|
| | |
|
| | | /**
|