mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
初始化项目
This commit is contained in:
+102
@@ -0,0 +1,102 @@
|
||||
package top.xyquan.ecomdmbbackend.utils;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
|
||||
/**
|
||||
* 当前用户上下文工具类
|
||||
* 提供更安全的当前用户信息获取方式
|
||||
*/
|
||||
@Component
|
||||
public class CurrentUserContext {
|
||||
|
||||
private final JwtUtils jwtUtils;
|
||||
|
||||
public CurrentUserContext(JwtUtils jwtUtils) {
|
||||
this.jwtUtils = jwtUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户Token负载信息
|
||||
*
|
||||
* @return Token负载信息
|
||||
* @throws BusinessException 如果无法获取用户信息
|
||||
*/
|
||||
public TokenPayload getCurrentUserPayload() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null || authentication.getCredentials() == null) {
|
||||
throw new BusinessException("用户未登录");
|
||||
}
|
||||
|
||||
String token = authentication.getCredentials().toString();
|
||||
return jwtUtils.getPayloadFromToken(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户公司ID
|
||||
*
|
||||
* @return 公司ID
|
||||
* @throws BusinessException 如果无法获取公司信息
|
||||
*/
|
||||
public Long getCurrentCompanyId() {
|
||||
TokenPayload payload = getCurrentUserPayload();
|
||||
if (payload.getCompanyId() == null) {
|
||||
throw new BusinessException("无法获取公司信息");
|
||||
}
|
||||
return payload.getCompanyId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户ID
|
||||
*
|
||||
* @return 用户ID
|
||||
* @throws BusinessException 如果无法获取用户信息
|
||||
*/
|
||||
public Long getCurrentUserId() {
|
||||
TokenPayload payload = getCurrentUserPayload();
|
||||
try {
|
||||
return Long.parseLong(payload.getUserId());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new BusinessException("用户ID格式错误");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户角色
|
||||
*
|
||||
* @return 用户角色
|
||||
*/
|
||||
public String getCurrentUserRole() {
|
||||
return getCurrentUserPayload().getRole();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户岗位ID
|
||||
*
|
||||
* @return 岗位ID
|
||||
*/
|
||||
public Long getCurrentUserPositionId() {
|
||||
return getCurrentUserPayload().getPositionId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户认证信息
|
||||
*
|
||||
* @return 认证信息
|
||||
*/
|
||||
public Authentication getCurrentAuthentication() {
|
||||
return SecurityContextHolder.getContext().getAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已登录
|
||||
*
|
||||
* @return 是否已登录
|
||||
*/
|
||||
public boolean isAuthenticated() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
return authentication != null && authentication.isAuthenticated();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user