mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-02 00:16:11 +08:00
Revert "refactor(sims): 重构JwtInterceptor并修复用户角色加载"
This reverts commit 8ee13b5f8f06a34f8c7f1fc41ae9d38169b25183.
This commit is contained in:
parent
8ee13b5f8f
commit
4579dbda81
@ -57,7 +57,4 @@ public class JwtInterceptor implements HandlerInterceptor {
|
|||||||
request.setAttribute("userId", userId);
|
request.setAttribute("userId", userId);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public static int getUserIdFromReq(HttpServletRequest request){
|
|
||||||
return (int) request.getAttribute("userId");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import top.suyiiyii.sims.common.AuthAccess;
|
import top.suyiiyii.sims.common.AuthAccess;
|
||||||
import top.suyiiyii.sims.common.JwtInterceptor;
|
|
||||||
import top.suyiiyii.sims.common.Result;
|
import top.suyiiyii.sims.common.Result;
|
||||||
import top.suyiiyii.sims.dto.CommonResponse;
|
import top.suyiiyii.sims.dto.CommonResponse;
|
||||||
import top.suyiiyii.sims.dto.UserDto;
|
import top.suyiiyii.sims.dto.UserDto;
|
||||||
@ -101,9 +100,8 @@ public class UserController {
|
|||||||
@Operation(description = "获取当前用户信息")
|
@Operation(description = "获取当前用户信息")
|
||||||
@AuthAccess(allowRoles = {"user"})
|
@AuthAccess(allowRoles = {"user"})
|
||||||
@GetMapping("/user/me")
|
@GetMapping("/user/me")
|
||||||
public Result<UserDto> getSelf(HttpServletRequest request) {
|
public Result<UserDto> getSelf() {
|
||||||
int userId = JwtInterceptor.getUserIdFromReq(request);
|
UserDto user = userService.findUser(0);
|
||||||
UserDto user = userService.findUser(userId);
|
|
||||||
return Result.success(user);
|
return Result.success(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,6 +124,14 @@ public class UserService {
|
|||||||
UserDto.setUserGroup(user.getUserGroup());
|
UserDto.setUserGroup(user.getUserGroup());
|
||||||
UserDto.setRoles(new ArrayList<>());
|
UserDto.setRoles(new ArrayList<>());
|
||||||
Integer id = user.getId();
|
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);
|
UserDtos.add(UserDto);
|
||||||
}
|
}
|
||||||
return UserDtos;
|
return UserDtos;
|
||||||
@ -133,15 +141,21 @@ public class UserService {
|
|||||||
|
|
||||||
UserDto UserDto = new UserDto();
|
UserDto UserDto = new UserDto();
|
||||||
User user = userMapper.selectById(id);
|
User user = userMapper.selectById(id);
|
||||||
if (user == null) {
|
|
||||||
throw new ServiceException("用户不存在");
|
|
||||||
}
|
|
||||||
UserDto.setUserId(user.getId());
|
UserDto.setUserId(user.getId());
|
||||||
UserDto.setUsername(user.getUsername());
|
UserDto.setUsername(user.getUsername());
|
||||||
UserDto.setGrade(user.getGrade());
|
UserDto.setGrade(user.getGrade());
|
||||||
UserDto.setUserGroup(user.getUserGroup());
|
UserDto.setUserGroup(user.getUserGroup());
|
||||||
UserDto.setRoles(new ArrayList<>());
|
UserDto.setRoles(new ArrayList<>());
|
||||||
//TODO: 获取用户角色
|
List<Role> roles = roleMapper.selectRolesById(id);
|
||||||
|
for (Role role : roles) {
|
||||||
|
Integer roleId = role.getId();
|
||||||
|
// 获取一个角色的名称列表
|
||||||
|
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
||||||
|
// 累加角色名称到用户的角色列表中
|
||||||
|
UserDto.getRoles().addAll(roleNameList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return UserDto;
|
return UserDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user