mirror of
				https://github.com/suyiiyii/SIMS.git
				synced 2025-11-05 00:04:54 +08:00 
			
		
		
		
	Reapply "refactor(sims): 重构JwtInterceptor并修复用户角色加载"
This reverts commit 4579dbda81299d207de88db0f073bc48301e931a.
This commit is contained in:
		
							parent
							
								
									4579dbda81
								
							
						
					
					
						commit
						9c61215678
					
				@ -57,4 +57,7 @@ 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,6 +8,7 @@ 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;
 | 
				
			||||||
@ -100,8 +101,9 @@ public class UserController {
 | 
				
			|||||||
    @Operation(description = "获取当前用户信息")
 | 
					    @Operation(description = "获取当前用户信息")
 | 
				
			||||||
    @AuthAccess(allowRoles = {"user"})
 | 
					    @AuthAccess(allowRoles = {"user"})
 | 
				
			||||||
    @GetMapping("/user/me")
 | 
					    @GetMapping("/user/me")
 | 
				
			||||||
    public Result<UserDto> getSelf() {
 | 
					    public Result<UserDto> getSelf(HttpServletRequest request) {
 | 
				
			||||||
        UserDto user = userService.findUser(0);
 | 
					        int userId = JwtInterceptor.getUserIdFromReq(request);
 | 
				
			||||||
 | 
					        UserDto user = userService.findUser(userId);
 | 
				
			||||||
        return Result.success(user);
 | 
					        return Result.success(user);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -124,14 +124,6 @@ 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;
 | 
				
			||||||
@ -141,21 +133,15 @@ 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<>());
 | 
				
			||||||
        List<Role> roles = roleMapper.selectRolesById(id);
 | 
					        //TODO: 获取用户角色
 | 
				
			||||||
        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