mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:09:19 +08:00
189 lines
5.0 KiB
Markdown
189 lines
5.0 KiB
Markdown
# CurrentUserContext 使用说明
|
|
|
|
## 概述
|
|
|
|
`CurrentUserContext` 是一个更安全的当前用户信息获取工具类,用于替代原有的 `SecurityUtils` 静态方法。它提供了更好的线程安全性和错误处理机制。
|
|
|
|
## 主要优势
|
|
|
|
### 1. 线程安全性
|
|
- 使用依赖注入管理 `JwtUtils` 实例,避免每次创建新实例
|
|
- 基于 Spring Security 的 `SecurityContextHolder`,天然线程安全
|
|
- 每个HTTP请求在独立线程中处理,用户信息完全隔离
|
|
|
|
### 2. 更好的错误处理
|
|
- 提供明确的异常信息
|
|
- 统一的错误处理机制
|
|
- 更好的调试体验
|
|
|
|
### 3. 依赖注入
|
|
- 符合Spring最佳实践
|
|
- 便于单元测试
|
|
- 更好的可维护性
|
|
|
|
## 使用方法
|
|
|
|
### 1. 在服务类中注入
|
|
|
|
```java
|
|
@Service
|
|
public class YourServiceImpl extends ServiceImpl<YourMapper, YourPO> implements YourService {
|
|
|
|
private final CurrentUserContext currentUserContext;
|
|
|
|
public YourServiceImpl(CurrentUserContext currentUserContext) {
|
|
this.currentUserContext = currentUserContext;
|
|
}
|
|
|
|
@Override
|
|
public YourVO createSomething(CreateDTO createDTO) {
|
|
// 获取当前用户公司ID
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
// 获取当前用户ID
|
|
Long userId = currentUserContext.getCurrentUserId();
|
|
|
|
// 业务逻辑...
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. 主要方法说明
|
|
|
|
#### getCurrentCompanyId()
|
|
```java
|
|
// 获取当前用户公司ID
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
// 如果无法获取,会抛出 BusinessException("无法获取公司信息")
|
|
```
|
|
|
|
#### getCurrentUserId()
|
|
```java
|
|
// 获取当前用户ID
|
|
Long userId = currentUserContext.getCurrentUserId();
|
|
// 如果无法获取,会抛出 BusinessException("用户未登录")
|
|
```
|
|
|
|
#### getCurrentAuthentication()
|
|
```java
|
|
// 获取当前用户认证信息
|
|
Authentication authentication = currentUserContext.getCurrentAuthentication();
|
|
```
|
|
|
|
#### isAuthenticated()
|
|
```java
|
|
// 检查是否已登录
|
|
boolean isLoggedIn = currentUserContext.isAuthenticated();
|
|
```
|
|
|
|
## 迁移指南
|
|
|
|
### 从 SecurityUtils 迁移到 CurrentUserContext
|
|
|
|
#### 原有代码:
|
|
```java
|
|
// 获取公司ID
|
|
Long companyId = SecurityUtils.getCurrentUserCompanyId();
|
|
if (companyId == null) {
|
|
throw new BusinessException("无法获取公司信息");
|
|
}
|
|
|
|
// 获取用户ID
|
|
Long userId = SecurityUtils.getCurrentUserId();
|
|
if (userId == null) {
|
|
throw new BusinessException("用户未登录");
|
|
}
|
|
```
|
|
|
|
#### 迁移后代码:
|
|
```java
|
|
// 获取公司ID(自动处理异常)
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
// 获取用户ID(自动处理异常)
|
|
Long userId = currentUserContext.getCurrentUserId();
|
|
```
|
|
|
|
## 安全特性
|
|
|
|
### 1. 请求隔离
|
|
```
|
|
Thread 1: User A -> SecurityContext A -> CompanyId A
|
|
Thread 2: User B -> SecurityContext B -> CompanyId B
|
|
Thread 3: User C -> SecurityContext C -> CompanyId C
|
|
```
|
|
|
|
### 2. 自动异常处理
|
|
- 用户未登录时自动抛出异常
|
|
- 公司信息获取失败时自动抛出异常
|
|
- 用户ID格式错误时自动抛出异常
|
|
|
|
### 3. 数据隔离
|
|
- 确保用户只能访问自己公司的数据
|
|
- 防止跨公司数据访问
|
|
- 自动添加公司ID过滤条件
|
|
|
|
## 最佳实践
|
|
|
|
### 1. 在服务层使用
|
|
```java
|
|
@Service
|
|
public class DepartmentServiceImpl {
|
|
|
|
private final CurrentUserContext currentUserContext;
|
|
|
|
public DepartmentServiceImpl(CurrentUserContext currentUserContext) {
|
|
this.currentUserContext = currentUserContext;
|
|
}
|
|
|
|
@Override
|
|
public DepartmentVO createDepartment(CreateDepartmentDTO dto) {
|
|
// 自动获取公司ID,无需手动处理
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
// 业务逻辑...
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. 在查询中强制添加公司过滤
|
|
```java
|
|
@Override
|
|
public PageResultVO<DepartmentVO> getDepartmentPage(PageQueryDTO pageQueryDTO, DepartmentFilterDTO queryDTO) {
|
|
// 强制添加公司ID过滤条件
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
LambdaQueryWrapper<DepartmentPO> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(DepartmentPO::getCompanyId, companyId); // 强制过滤
|
|
|
|
// 其他过滤条件...
|
|
}
|
|
```
|
|
|
|
### 3. 在更新/删除时验证权限
|
|
```java
|
|
@Override
|
|
public void deleteDepartment(Long id) {
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
// 验证数据是否属于当前公司
|
|
DepartmentPO department = this.getById(id);
|
|
if (!companyId.equals(department.getCompanyId())) {
|
|
throw new BusinessException("无权操作此部门");
|
|
}
|
|
|
|
// 执行删除操作...
|
|
}
|
|
```
|
|
|
|
## 注意事项
|
|
|
|
1. **不要在控制器层使用**:控制器应该保持简洁,只负责请求处理
|
|
2. **统一异常处理**:所有异常都会被全局异常处理器捕获
|
|
3. **测试友好**:便于在单元测试中模拟用户上下文
|
|
4. **性能优化**:避免重复的token解析操作
|
|
|
|
## 总结
|
|
|
|
`CurrentUserContext` 提供了更安全、更易用的用户信息获取方式,是企业级应用中处理多租户数据隔离的最佳实践。
|