You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
package com.wrj.driver.server.util;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.wrj.driver.server.constant.ErrorCode;
|
|
import com.wrj.driver.server.dto.SecurityUser;
|
|
import com.wrj.driver.server.entity.SysUser;
|
|
import com.wrj.driver.server.exception.BusinessException;
|
|
import com.wrj.driver.server.redis.RedisCache;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
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, "尚未登录");
|
|
SecurityUser securityUser = new SecurityUser();
|
|
SysUser tdSysUser = redisCache.getCacheObject(token);
|
|
|
|
if (Objects.isNull(tdSysUser)){
|
|
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;
|
|
}
|
|
}
|
|
|