mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-05-31 11:46:42 +08:00
实现获取用户基本信息 (#25)
* Reapply "refactor(sims): 重构JwtInterceptor并修复用户角色加载" This reverts commit 4579dbda81299d207de88db0f073bc48301e931a. * 给注册添加参数校验
This commit is contained in:
parent
4579dbda81
commit
2024c5e07f
4
pom.xml
4
pom.xml
@ -123,6 +123,10 @@
|
|||||||
<version>4.0.0</version>
|
<version>4.0.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,25 @@
|
|||||||
package top.suyiiyii.sims.controller;
|
package top.suyiiyii.sims.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
import org.hibernate.validator.constraints.Range;
|
||||||
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;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
import top.suyiiyii.sims.exception.ServiceException;
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
|
import top.suyiiyii.sims.mapper.MpUserMapper;
|
||||||
import top.suyiiyii.sims.service.RoleService;
|
import top.suyiiyii.sims.service.RoleService;
|
||||||
import top.suyiiyii.sims.service.UserService;
|
import top.suyiiyii.sims.service.UserService;
|
||||||
|
|
||||||
@ -34,12 +42,14 @@ public class UserController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
UserService userService;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
MpUserMapper mpUserMapper;
|
||||||
|
@Autowired
|
||||||
RoleService roleService;
|
RoleService roleService;
|
||||||
|
|
||||||
|
|
||||||
@AuthAccess(allowRoles = {"guest"})
|
@AuthAccess(allowRoles = {"guest"})
|
||||||
@PostMapping("/user/login")
|
@PostMapping("/user/login")
|
||||||
public Result<LoginResponse> login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest) {
|
public Result<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||||
log.info("login request:{}", request);
|
log.info("login request:{}", request);
|
||||||
|
|
||||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||||
@ -57,14 +67,20 @@ public class UserController {
|
|||||||
|
|
||||||
@AuthAccess(allowRoles = {"guest"})
|
@AuthAccess(allowRoles = {"guest"})
|
||||||
@PostMapping("/user/register")
|
@PostMapping("/user/register")
|
||||||
public Result<CommonResponse> register(@RequestBody RegisterRequest request) {
|
public Result<CommonResponse> register(@RequestBody @Valid
|
||||||
|
RegisterRequest request) {
|
||||||
log.info("register request:{}", request);
|
log.info("register request:{}", request);
|
||||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
// 检查 username 是否已存在
|
||||||
|
if (mpUserMapper.selectOne(new LambdaQueryWrapper<User>(User.class).eq(User::getUsername, request.getUsername())) != null) {
|
||||||
return Result.error("用户名或密码不能为空");
|
throw new ServiceException("用户名已存在");
|
||||||
}
|
}
|
||||||
if (request.getPassword() == null || request.getPassword().length() < 3) {
|
// 检查 studentId 是否已存在
|
||||||
throw new ServiceException("密码长度不能小于3位");
|
if (mpUserMapper.selectOne(new LambdaQueryWrapper<User>(User.class).eq(User::getStudentId, request.getStudentId())) != null) {
|
||||||
|
throw new ServiceException("学号已存在");
|
||||||
|
}
|
||||||
|
// 检查 email 是否已存在
|
||||||
|
if (mpUserMapper.selectOne(new LambdaQueryWrapper<User>(User.class).eq(User::getEmail, request.getEmail())) != null) {
|
||||||
|
throw new ServiceException("邮箱已存在");
|
||||||
}
|
}
|
||||||
userService.register(request);
|
userService.register(request);
|
||||||
|
|
||||||
@ -100,19 +116,26 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class RegisterRequest {
|
public static class RegisterRequest {
|
||||||
|
@Length(min = 3, max = 20)
|
||||||
private String username;
|
private String username;
|
||||||
|
@Length(min = 6, max = 20)
|
||||||
private String password;
|
private String password;
|
||||||
|
@Range(min = 1, max = 1000000000)
|
||||||
private Integer studentId;
|
private Integer studentId;
|
||||||
|
@Email
|
||||||
private String email;
|
private String email;
|
||||||
|
@Length(min = 1, max = 20)
|
||||||
private String grade;
|
private String grade;
|
||||||
|
@Length(min = 1, max = 20)
|
||||||
private String userGroup;
|
private String userGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,10 +35,8 @@ public class User {
|
|||||||
@UniqueIndex
|
@UniqueIndex
|
||||||
@Column(comment = "邮箱", notNull = true)
|
@Column(comment = "邮箱", notNull = true)
|
||||||
private String email;
|
private String email;
|
||||||
@UniqueIndex
|
|
||||||
@Column(comment = "年级", notNull = true)
|
@Column(comment = "年级", notNull = true)
|
||||||
private String grade;
|
private String grade;
|
||||||
@UniqueIndex
|
|
||||||
@Column(comment = "用户所属团队", notNull = true)
|
@Column(comment = "用户所属团队", notNull = true)
|
||||||
private String userGroup;
|
private String userGroup;
|
||||||
}
|
}
|
||||||
|
@ -78,25 +78,9 @@ public class UserService {
|
|||||||
public void register(UserController.RegisterRequest req) {
|
public void register(UserController.RegisterRequest req) {
|
||||||
|
|
||||||
User dbUser = userMapper.selectByUserId(req.getStudentId());
|
User dbUser = userMapper.selectByUserId(req.getStudentId());
|
||||||
|
|
||||||
if (req.getUsername() == null || req.getUsername().equals("")) {
|
|
||||||
throw new ServiceException("用户名不能为空");
|
|
||||||
}
|
|
||||||
if (dbUser != null) {
|
if (dbUser != null) {
|
||||||
throw new ServiceException("账号已经存在");
|
throw new ServiceException("账号已经存在");
|
||||||
}
|
}
|
||||||
if (req.getStudentId() == null || req.getStudentId().equals("")) {
|
|
||||||
throw new ServiceException("学号不能为空");
|
|
||||||
}
|
|
||||||
if (req.getPassword() == null || req.getPassword().equals("")) {
|
|
||||||
throw new ServiceException("密码不能为空");
|
|
||||||
}
|
|
||||||
if (req.getEmail() == null || req.getEmail().equals("")) {
|
|
||||||
throw new ServiceException("邮箱不能为空");
|
|
||||||
}
|
|
||||||
if (req.getUserGroup() == null || req.getUserGroup().equals("")) {
|
|
||||||
throw new ServiceException("组别不能为空");
|
|
||||||
}
|
|
||||||
User user = modelMapper.map(req, User.class);
|
User user = modelMapper.map(req, User.class);
|
||||||
|
|
||||||
mpUserMapper.insert(user);
|
mpUserMapper.insert(user);
|
||||||
@ -124,14 +108,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 +117,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