driver-server/src/main/java/com/jwl/driver/server/util/SecurityUtil.java

66 lines
1.9 KiB
Java
Raw Normal View History

2023-08-12 14:42:57 +08:00
package com.jwl.driver.server.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.jwl.driver.server.constant.ErrorCode;
import com.jwl.driver.server.dto.SecurityUser;
import com.jwl.driver.server.entity.TdSysUser;
import com.jwl.driver.server.exception.BusinessException;
import com.jwl.driver.server.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
*
*/
public class SecurityUtil {
private static final Logger log = LoggerFactory.getLogger(SecurityUtil.class);
private static final RedisCache redisCache = SpringAsyncUtil.<RedisCache>getBean(RedisCache.class);
/**
*
* @return
*/
public static SecurityUser getLoginUser() {
String token = TokenThreadUtil.getToken();
if (StrUtil.isBlank(token))
throw new BusinessException(ErrorCode.AUTH_ERROR, "尚未登录");
2023-08-15 01:37:50 +08:00
SecurityUser securityUser = new SecurityUser();
2023-08-12 14:42:57 +08:00
TdSysUser tdSysUser = redisCache.getCacheObject(token);
2023-08-15 01:37:50 +08:00
if (Objects.isNull(tdSysUser)){
2023-08-12 14:42:57 +08:00
throw new BusinessException(ErrorCode.AUTH_ERROR, "登录信息已失效");
}
BeanUtils.copyProperties(tdSysUser,securityUser);
securityUser.setToken(token);
return securityUser;
}
/**
*
* @return
*/
public static Long getUserId() {
Long userId = null;
SecurityUser loginUser = SecurityUtil.getLoginUser();
if (null != loginUser) {
userId = loginUser.getUserId();
}
return userId;
}
}