mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-02 00:16:11 +08:00
给注册添加参数校验
This commit is contained in:
parent
9c61215678
commit
feff994f0f
4
pom.xml
4
pom.xml
@ -123,6 +123,10 @@
|
||||
<version>4.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -1,10 +1,15 @@
|
||||
package top.suyiiyii.sims.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Email;
|
||||
import lombok.Data;
|
||||
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.web.bind.annotation.*;
|
||||
import top.suyiiyii.sims.common.AuthAccess;
|
||||
@ -12,7 +17,9 @@ import top.suyiiyii.sims.common.JwtInterceptor;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.dto.CommonResponse;
|
||||
import top.suyiiyii.sims.dto.UserDto;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.mapper.MpUserMapper;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
import top.suyiiyii.sims.service.UserService;
|
||||
|
||||
@ -35,12 +42,14 @@ public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
MpUserMapper mpUserMapper;
|
||||
@Autowired
|
||||
RoleService roleService;
|
||||
|
||||
|
||||
@AuthAccess(allowRoles = {"guest"})
|
||||
@PostMapping("/user/login")
|
||||
public Result<LoginResponse> login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest) {
|
||||
public Result<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
log.info("login request:{}", request);
|
||||
|
||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||
@ -58,14 +67,20 @@ public class UserController {
|
||||
|
||||
@AuthAccess(allowRoles = {"guest"})
|
||||
@PostMapping("/user/register")
|
||||
public Result<CommonResponse> register(@RequestBody RegisterRequest request) {
|
||||
public Result<CommonResponse> register(@RequestBody @Valid
|
||||
RegisterRequest request) {
|
||||
log.info("register request:{}", request);
|
||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||
|
||||
return Result.error("用户名或密码不能为空");
|
||||
// 检查 username 是否已存在
|
||||
if (mpUserMapper.selectOne(new LambdaQueryWrapper<User>(User.class).eq(User::getUsername, request.getUsername())) != null) {
|
||||
throw new ServiceException("用户名已存在");
|
||||
}
|
||||
if (request.getPassword() == null || request.getPassword().length() < 3) {
|
||||
throw new ServiceException("密码长度不能小于3位");
|
||||
// 检查 studentId 是否已存在
|
||||
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);
|
||||
|
||||
@ -110,11 +125,17 @@ public class UserController {
|
||||
|
||||
@Data
|
||||
public static class RegisterRequest {
|
||||
@Length(min = 3, max = 20)
|
||||
private String username;
|
||||
@Length(min = 6, max = 20)
|
||||
private String password;
|
||||
@Range(min = 1, max = 1000000000)
|
||||
private Integer studentId;
|
||||
@Email
|
||||
private String email;
|
||||
@Length(min = 1, max = 20)
|
||||
private String grade;
|
||||
@Length(min = 1, max = 20)
|
||||
private String userGroup;
|
||||
}
|
||||
|
||||
|
@ -35,10 +35,8 @@ public class User {
|
||||
@UniqueIndex
|
||||
@Column(comment = "邮箱", notNull = true)
|
||||
private String email;
|
||||
@UniqueIndex
|
||||
@Column(comment = "年级", notNull = true)
|
||||
private String grade;
|
||||
@UniqueIndex
|
||||
@Column(comment = "用户所属团队", notNull = true)
|
||||
private String userGroup;
|
||||
}
|
||||
|
@ -78,25 +78,9 @@ public class UserService {
|
||||
public void register(UserController.RegisterRequest req) {
|
||||
|
||||
User dbUser = userMapper.selectByUserId(req.getStudentId());
|
||||
|
||||
if (req.getUsername() == null || req.getUsername().equals("")) {
|
||||
throw new ServiceException("用户名不能为空");
|
||||
}
|
||||
if (dbUser != null) {
|
||||
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);
|
||||
|
||||
mpUserMapper.insert(user);
|
||||
|
Loading…
x
Reference in New Issue
Block a user