修复:优化权限反馈
This commit is contained in:
@@ -638,8 +638,8 @@ export const adminDashboardApi = {
|
||||
}
|
||||
|
||||
export interface NailArtistInfoVO {
|
||||
id: number
|
||||
userId: number
|
||||
id: number | string
|
||||
userId: number | string
|
||||
identity: string
|
||||
experience: number
|
||||
skills: string
|
||||
@@ -647,7 +647,7 @@ export interface NailArtistInfoVO {
|
||||
status: number
|
||||
portfolio: string
|
||||
nickname: string
|
||||
shopId: number
|
||||
shopId: number | string
|
||||
shopName: string
|
||||
}
|
||||
|
||||
|
||||
+25
-34
@@ -1,8 +1,11 @@
|
||||
package com.xiaowang.yuminailcore.aspect;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.xiaowang.yuminailcore.annotation.RequirePermission;
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.entity.PermissionDO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.mapper.PermissionMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.UserMapper;
|
||||
import com.xiaowang.yuminailcore.service.PermissionService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -11,6 +14,7 @@ import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@@ -22,72 +26,59 @@ import java.lang.reflect.Method;
|
||||
@RequiredArgsConstructor
|
||||
public class PermissionAspect {
|
||||
|
||||
// 注入权限服务
|
||||
private final PermissionService permissionService;
|
||||
// 注入用户数据访问对象
|
||||
private final PermissionMapper permissionMapper;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 环绕通知,用于检查权限
|
||||
* @param joinPoint 连接点,可以获取方法执行的相关信息
|
||||
* @return 方法执行结果或错误信息
|
||||
* @throws Throwable 方法执行可能抛出的异常
|
||||
*/
|
||||
@Around("@annotation(com.xiaowang.yuminailcore.annotation.RequirePermission)")
|
||||
public Object checkPermission(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
// 获取请求属性
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
// 如果无法获取请求信息,返回错误
|
||||
if (attributes == null) {
|
||||
return Result.error(403, "无法获取请求信息");
|
||||
}
|
||||
|
||||
// 获取HTTP请求对象
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
// 从请求中获取用户ID
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
|
||||
// 如果用户ID为空,表示未登录
|
||||
if (userId == null) {
|
||||
return Result.error(401, "未登录");
|
||||
}
|
||||
|
||||
// 获取方法签名
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
// 获取目标方法
|
||||
Method method = signature.getMethod();
|
||||
// 获取方法上的权限注解
|
||||
RequirePermission requirePermission = method.getAnnotation(RequirePermission.class);
|
||||
|
||||
// 如果方法没有权限注解,直接执行方法
|
||||
if (requirePermission == null) {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
// 根据用户ID查询用户信息
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
// 如果用户不存在,返回错误
|
||||
if (userDO == null) {
|
||||
return Result.error(404, "用户不存在");
|
||||
}
|
||||
|
||||
// 获取权限码
|
||||
String permissionCode = requirePermission.value();
|
||||
|
||||
// 默认有权限
|
||||
boolean hasPermission = true;
|
||||
|
||||
// 如果权限码不为空,检查用户是否拥有该权限
|
||||
if (!permissionCode.isEmpty()) {
|
||||
hasPermission = permissionService.hasPermission(userId, permissionCode);
|
||||
}
|
||||
|
||||
// 如果没有权限,返回错误
|
||||
boolean hasPermission = !StringUtils.hasText(permissionCode)
|
||||
|| permissionService.hasPermission(userId, permissionCode);
|
||||
if (!hasPermission) {
|
||||
return Result.error(403, "权限不足");
|
||||
return Result.error(403, buildPermissionDeniedMessage(permissionCode));
|
||||
}
|
||||
|
||||
// 有权限,执行目标方法
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
private String buildPermissionDeniedMessage(String permissionCode) {
|
||||
if (!StringUtils.hasText(permissionCode)) {
|
||||
return "权限不足";
|
||||
}
|
||||
|
||||
PermissionDO permissionDO = permissionMapper.selectOne(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getPermissionCode, permissionCode)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
.last("limit 1")
|
||||
);
|
||||
if (permissionDO != null && StringUtils.hasText(permissionDO.getPermissionName())) {
|
||||
return "权限不足:缺少权限【" + permissionDO.getPermissionName() + "】(" + permissionCode + ")";
|
||||
}
|
||||
return "权限不足:缺少权限代码 " + permissionCode;
|
||||
}
|
||||
}
|
||||
|
||||
+41
-64
@@ -5,117 +5,94 @@ import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.NailArtistInfoDTO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.service.NailArtistInfoService;
|
||||
import com.xiaowang.yuminailcore.service.UserService;
|
||||
import com.xiaowang.yuminailcore.support.AdminScopeHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 美甲师管理Controller
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/nail-artist")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminNailArtistController {
|
||||
|
||||
@Autowired
|
||||
private NailArtistInfoService nailArtistInfoService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private AdminScopeHelper adminScopeHelper;
|
||||
|
||||
/**
|
||||
* 获取所有美甲师信息
|
||||
* 权限:管理员或店长
|
||||
*/
|
||||
private static final String USER_NOT_FOUND_MESSAGE = "用户信息不存在";
|
||||
private static final String VIEW_SCOPE_MESSAGE = "当前登录店铺无权查看该美甲师,请切换到所属店铺后重试";
|
||||
private static final String EDIT_SCOPE_MESSAGE = "当前登录店铺无权编辑该美甲师,请切换到所属店铺后重试";
|
||||
|
||||
private final NailArtistInfoService nailArtistInfoService;
|
||||
private final AdminScopeHelper adminScopeHelper;
|
||||
|
||||
@RequirePermission("nail_artist:view")
|
||||
@GetMapping("/list")
|
||||
public Result<List<NailArtistInfoDTO>> getAllNailArtists(HttpServletRequest request) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
|
||||
if (currentUser == null) {
|
||||
return Result.error("用户信息不存在");
|
||||
return Result.error(USER_NOT_FOUND_MESSAGE);
|
||||
}
|
||||
|
||||
|
||||
List<NailArtistInfoDTO> nailArtistInfoDTOList;
|
||||
|
||||
// 管理员可以查看所有美甲师,店长只能查看本店铺的美甲师
|
||||
if (currentUser.getType() == 4) { // 管理员
|
||||
if (Integer.valueOf(4).equals(currentUser.getType())) {
|
||||
nailArtistInfoDTOList = nailArtistInfoService.getAllNailArtists();
|
||||
} else if (currentUser.getType() == 2) { // 店长
|
||||
} else if (Integer.valueOf(2).equals(currentUser.getType())) {
|
||||
nailArtistInfoDTOList = nailArtistInfoService.getByShopId(currentUser.getShopId());
|
||||
} else {
|
||||
return Result.error("无权限访问");
|
||||
return Result.error("权限不足:缺少权限【查看美甲师】(nail_artist:view)");
|
||||
}
|
||||
|
||||
|
||||
return Result.success(nailArtistInfoDTOList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID获取美甲师信息
|
||||
* 权限:管理员、店长或美甲师本人
|
||||
*/
|
||||
|
||||
@RequirePermission("nail_artist:view")
|
||||
@GetMapping("/get/{userId}")
|
||||
public Result<NailArtistInfoDTO> getNailArtistInfo(@PathVariable Long userId, HttpServletRequest request) {
|
||||
Long currentUserId = (Long) request.getAttribute("userId");
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
|
||||
if (currentUser == null) {
|
||||
return Result.error("用户信息不存在");
|
||||
return Result.error(USER_NOT_FOUND_MESSAGE);
|
||||
}
|
||||
|
||||
// 检查权限:管理员可以查看所有,店长只能查看本店铺的,美甲师只能查看自己的
|
||||
if (currentUser.getType() != 4) { // 不是管理员
|
||||
if (currentUser.getType() == 2) { // 店长
|
||||
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, userId, "无权限访问");
|
||||
} else if (currentUser.getType() == 3) { // 美甲师
|
||||
// 只能查看自己的信息
|
||||
|
||||
if (!Integer.valueOf(4).equals(currentUser.getType())) {
|
||||
if (Integer.valueOf(2).equals(currentUser.getType())) {
|
||||
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, userId, VIEW_SCOPE_MESSAGE);
|
||||
} else if (Integer.valueOf(3).equals(currentUser.getType())) {
|
||||
if (!currentUser.getId().equals(userId)) {
|
||||
return Result.error("无权限访问");
|
||||
return Result.error(VIEW_SCOPE_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
return Result.error("无权限访问");
|
||||
return Result.error("权限不足:缺少权限【查看美甲师】(nail_artist:view)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NailArtistInfoDTO nailArtistInfoDTO = nailArtistInfoService.getByUserId(userId);
|
||||
return Result.success(nailArtistInfoDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存或更新美甲师信息
|
||||
* 权限:管理员、店长或美甲师本人
|
||||
*/
|
||||
|
||||
@RequirePermission("nail_artist:edit")
|
||||
@PostMapping("/save")
|
||||
public Result<NailArtistInfoDTO> saveNailArtistInfo(@RequestBody NailArtistInfoDTO nailArtistInfoDTO, HttpServletRequest request) {
|
||||
Long currentUserId = (Long) request.getAttribute("userId");
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
|
||||
if (currentUser == null) {
|
||||
return Result.error("用户信息不存在");
|
||||
return Result.error(USER_NOT_FOUND_MESSAGE);
|
||||
}
|
||||
|
||||
// 检查权限:管理员可以编辑所有,店长只能编辑本店铺的,美甲师只能编辑自己的
|
||||
if (currentUser.getType() != 4) { // 不是管理员
|
||||
if (currentUser.getType() == 2) { // 店长
|
||||
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, nailArtistInfoDTO.getUserId(), "无权限编辑");
|
||||
} else if (currentUser.getType() == 3) { // 美甲师
|
||||
// 只能编辑自己的信息
|
||||
|
||||
if (!Integer.valueOf(4).equals(currentUser.getType())) {
|
||||
if (Integer.valueOf(2).equals(currentUser.getType())) {
|
||||
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, nailArtistInfoDTO.getUserId(), EDIT_SCOPE_MESSAGE);
|
||||
} else if (Integer.valueOf(3).equals(currentUser.getType())) {
|
||||
if (!currentUser.getId().equals(nailArtistInfoDTO.getUserId())) {
|
||||
return Result.error("无权限编辑");
|
||||
return Result.error(EDIT_SCOPE_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
return Result.error("无权限编辑");
|
||||
return Result.error("权限不足:缺少权限【编辑美甲师】(nail_artist:edit)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NailArtistInfoDTO saved = nailArtistInfoService.saveOrUpdateNailArtistInfo(nailArtistInfoDTO);
|
||||
return Result.success(saved);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -12,11 +14,13 @@ public class NailArtistInfoDTO {
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 关联用户表主键(美甲师用户)
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
@@ -57,6 +61,7 @@ public class NailArtistInfoDTO {
|
||||
/**
|
||||
* 所属店铺ID
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long shopId;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# 2026-03-27 权限不足提示细化
|
||||
|
||||
## 本次目标
|
||||
|
||||
- 当前前端已经会直接展示后端返回的 `message`,但后端很多场景只返回“权限不足”或“无权限编辑”,定位成本太高。
|
||||
- 本轮把“缺少功能权限”和“当前店铺范围不允许操作”两类提示区分开,让前端能直接看到更具体的原因。
|
||||
|
||||
## 本次调整
|
||||
|
||||
- 重写 [PermissionAspect.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/aspect/PermissionAspect.java):
|
||||
- 缺少 `@RequirePermission` 对应权限时,不再只返回“权限不足”。
|
||||
- 改为优先读取权限表中的权限名称,返回格式例如:
|
||||
- `权限不足:缺少权限【编辑美甲师】(nail_artist:edit)`
|
||||
- 如果权限表中暂时找不到权限名称,则回退为:
|
||||
- `权限不足:缺少权限代码 nail_artist:edit`
|
||||
|
||||
- 重写 [AdminNailArtistController.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/AdminNailArtistController.java) 中的提示:
|
||||
- 店铺范围不匹配时:
|
||||
- `当前登录店铺无权编辑该美甲师,请切换到所属店铺后重试`
|
||||
- `当前登录店铺无权查看该美甲师,请切换到所属店铺后重试`
|
||||
- 非后台角色误入接口时:
|
||||
- `权限不足:缺少权限【查看美甲师】(nail_artist:view)`
|
||||
- `权限不足:缺少权限【编辑美甲师】(nail_artist:edit)`
|
||||
|
||||
## 结果
|
||||
|
||||
- 前端不需要额外改弹窗逻辑,继续展示后端 `message` 即可。
|
||||
- 当问题来自“权限没分到”时,你可以直接看到缺的是哪一项权限。
|
||||
- 当问题来自“当前登录店铺和目标数据不匹配”时,也能直接区分,不会再和权限码缺失混在一起。
|
||||
|
||||
## 验证
|
||||
|
||||
- 后端执行 `mvn -q -DskipTests compile`。
|
||||
- 前端执行 `npm run build`。
|
||||
@@ -0,0 +1,33 @@
|
||||
# 2026-03-27 雪花ID精度导致美甲师编辑鉴权失败修复
|
||||
|
||||
## 问题现象
|
||||
|
||||
- 店长在后台编辑本店美甲师时,前端提交的载荷中 `userId` 看似正确,但后端仍返回 `{"code":500,"message":"无权限编辑"}`。
|
||||
- 前端控制台里可见 `userId` 为超大雪花 ID,例如 `2037353373588979700`。
|
||||
|
||||
## 根因分析
|
||||
|
||||
- 雪花 ID 超过了 JavaScript `Number` 的安全整数范围。
|
||||
- 美甲师列表接口返回的 `userId`、`id`、`shopId` 没有统一按字符串序列化,浏览器收到后会以普通数字处理并发生精度丢失。
|
||||
- 编辑弹窗再把这个失真的 `userId` 提交给后端时,后端店铺范围校验会拿着错误的用户 ID 判断绑定关系,最终误报“无权限编辑”。
|
||||
|
||||
## 本次修复
|
||||
|
||||
- 在 [NailArtistInfoDTO.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/dto/NailArtistInfoDTO.java) 中,为以下 Long 字段增加字符串序列化:
|
||||
- `id`
|
||||
- `userId`
|
||||
- `shopId`
|
||||
- 在 [admin.ts](/E:/code/xiaowang/Frontend/src/api/admin.ts) 中,将 `NailArtistInfoVO` 的对应 ID 字段类型放宽为 `string | number`,避免前端继续以安全整数假设处理雪花 ID。
|
||||
- 结合上一轮对编辑弹窗草稿恢复逻辑的修复,确保当前美甲师标识不会被本地暂存覆盖,也不会被数字精度破坏。
|
||||
|
||||
## 影响范围
|
||||
|
||||
- 不涉及 SQL 变更。
|
||||
- 直接影响后台美甲师列表与编辑链路的 ID 精度安全。
|
||||
- 这类问题在其他仍以裸 `Long` 返回且前端按 `number` 接收的接口中也存在同类风险,后续建议继续统一复核雪花 ID 返回口径。
|
||||
|
||||
## 验证
|
||||
|
||||
- 后端执行 `mvn -q -DskipTests compile`。
|
||||
- 前端执行 `npm run build`。
|
||||
- 复测后台“店长编辑本店美甲师”时,请关注网络面板中的 `userId` 是否已经变为字符串形式返回与提交。
|
||||
Reference in New Issue
Block a user