mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-07-16 07:53:01 +08:00
refactor(sims): 重构JwtInterceptor并修复用户角色加载
- 在JwtInterceptor中新增getUserIdFromReq方法,用于从请求中获取用户ID。 - 在UserController的getSelf方法中使用新方法获取当前用户ID并修复。 - 重构UserService中关于用户角色加载的代码,消除不必要的循环,提高执行效率。 - 修复了在UserService中findUser方法传入空值的问题,并增加用户不存在时的异常处理。
This commit is contained in:
parent
28f5b146d8
commit
8ee13b5f8f
@ -57,4 +57,7 @@ public class JwtInterceptor implements HandlerInterceptor {
|
||||
request.setAttribute("userId", userId);
|
||||
return true;
|
||||
}
|
||||
public static int getUserIdFromReq(HttpServletRequest request){
|
||||
return (int) request.getAttribute("userId");
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.suyiiyii.sims.common.AuthAccess;
|
||||
import top.suyiiyii.sims.common.JwtInterceptor;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.dto.CommonResponse;
|
||||
import top.suyiiyii.sims.dto.UserDto;
|
||||
@ -100,8 +101,9 @@ public class UserController {
|
||||
@Operation(description = "获取当前用户信息")
|
||||
@AuthAccess(allowRoles = {"user"})
|
||||
@GetMapping("/user/me")
|
||||
public Result<UserDto> getSelf() {
|
||||
UserDto user = userService.findUser(0);
|
||||
public Result<UserDto> getSelf(HttpServletRequest request) {
|
||||
int userId = JwtInterceptor.getUserIdFromReq(request);
|
||||
UserDto user = userService.findUser(userId);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
|
@ -124,14 +124,6 @@ public class UserService {
|
||||
UserDto.setUserGroup(user.getUserGroup());
|
||||
UserDto.setRoles(new ArrayList<>());
|
||||
Integer id = user.getId();
|
||||
List<Role> roles = roleMapper.selectRolesById(id);
|
||||
for (Role role : roles) {
|
||||
Integer roleId = role.getId();
|
||||
// 获取一个角色的名称列表
|
||||
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
||||
// 累加角色名称到用户的角色列表中
|
||||
UserDto.getRoles().addAll(roleNameList);
|
||||
}
|
||||
UserDtos.add(UserDto);
|
||||
}
|
||||
return UserDtos;
|
||||
@ -141,21 +133,15 @@ public class UserService {
|
||||
|
||||
UserDto UserDto = new UserDto();
|
||||
User user = userMapper.selectById(id);
|
||||
if (user == null) {
|
||||
throw new ServiceException("用户不存在");
|
||||
}
|
||||
UserDto.setUserId(user.getId());
|
||||
UserDto.setUsername(user.getUsername());
|
||||
UserDto.setGrade(user.getGrade());
|
||||
UserDto.setUserGroup(user.getUserGroup());
|
||||
UserDto.setRoles(new ArrayList<>());
|
||||
List<Role> roles = roleMapper.selectRolesById(id);
|
||||
for (Role role : roles) {
|
||||
Integer roleId = role.getId();
|
||||
// 获取一个角色的名称列表
|
||||
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
||||
// 累加角色名称到用户的角色列表中
|
||||
UserDto.getRoles().addAll(roleNameList);
|
||||
}
|
||||
|
||||
|
||||
//TODO: 获取用户角色
|
||||
return UserDto;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user