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.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; } }