feat(auth): 优化注册功能并添加注册异常处理
- 新增 RegistrationException 类用于处理注册相关的异常 - 在全局异常处理器中添加 RegistrationException 的处理逻辑 - 修改用户服务中的注册逻辑,当用户名或邮箱已存在时抛出 RegistrationException - 更新注册码实体类,将日期时间字段改为字符串类型 - 调整注册码生成和验证逻辑,使用字符串格式的日期时间
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package com.test.bijihoudaun.common.advice;
|
package com.test.bijihoudaun.common.advice;
|
||||||
|
|
||||||
import com.test.bijihoudaun.common.exception.BaseException;
|
import com.test.bijihoudaun.common.exception.BaseException;
|
||||||
|
import com.test.bijihoudaun.common.exception.RegistrationException;
|
||||||
import com.test.bijihoudaun.common.response.R;
|
import com.test.bijihoudaun.common.response.R;
|
||||||
import com.test.bijihoudaun.common.response.ResultCode;
|
import com.test.bijihoudaun.common.response.ResultCode;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -33,6 +33,15 @@ public class GlobalExceptionHandler {
|
|||||||
return R.fail(e.getResultCode().getCode(), e.getMessage());
|
return R.fail(e.getResultCode().getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理注册异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(RegistrationException.class)
|
||||||
|
public R<String> handleRegistrationException(RegistrationException e) {
|
||||||
|
log.warn("Registration attempt failed: {}", e.getMessage());
|
||||||
|
return R.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理文件大小超出限制异常
|
* 处理文件大小超出限制异常
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.test.bijihoudaun.common.exception;
|
||||||
|
|
||||||
|
public class RegistrationException extends RuntimeException {
|
||||||
|
public RegistrationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("registration_codes")
|
@TableName("registration_codes")
|
||||||
@@ -24,11 +23,51 @@ public class RegistrationCode implements Serializable {
|
|||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
@Schema(description = "过期时间", name = "expiryTime")
|
@Schema(description = "过期时间", name = "expiryTime")
|
||||||
private LocalDateTime expiryTime;
|
private String expiryTime;
|
||||||
|
|
||||||
@Schema(description = "创建者", name = "createdBy")
|
@Schema(description = "创建者", name = "createdBy")
|
||||||
private String createdBy;
|
private String createdBy;
|
||||||
|
|
||||||
@Schema(description = "创建时间", name = "createdAt")
|
@Schema(description = "创建时间", name = "createdAt")
|
||||||
private LocalDateTime createdAt;
|
private String createdAt;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExpiryTime() {
|
||||||
|
return expiryTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpiryTime(String expiryTime) {
|
||||||
|
this.expiryTime = expiryTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreatedBy() {
|
||||||
|
return createdBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedBy(String createdBy) {
|
||||||
|
this.createdBy = createdBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedAt(String createdAt) {
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -21,7 +22,8 @@ public class RegistrationCodeServiceImpl extends ServiceImpl<RegistrationCodeMap
|
|||||||
String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
|
String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
|
||||||
registrationCode.setCode(code);
|
registrationCode.setCode(code);
|
||||||
registrationCode.setCreatedBy(creator);
|
registrationCode.setCreatedBy(creator);
|
||||||
registrationCode.setExpiryTime(LocalDateTime.now().plusDays(1));
|
registrationCode.setCreatedAt(LocalDateTime.now().toString());
|
||||||
|
registrationCode.setExpiryTime(LocalDateTime.now().plusDays(1).toString());
|
||||||
save(registrationCode);
|
save(registrationCode);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
@@ -37,7 +39,8 @@ public class RegistrationCodeServiceImpl extends ServiceImpl<RegistrationCodeMap
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (registrationCode.getExpiryTime().isBefore(LocalDateTime.now())) {
|
LocalDateTime expiryTime = LocalDateTime.parse(registrationCode.getExpiryTime());
|
||||||
|
if (expiryTime.isBefore(LocalDateTime.now())) {
|
||||||
remove(queryWrapper); // 注册码过期,删除
|
remove(queryWrapper); // 注册码过期,删除
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -50,6 +53,6 @@ public class RegistrationCodeServiceImpl extends ServiceImpl<RegistrationCodeMap
|
|||||||
@Override
|
@Override
|
||||||
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
|
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
|
||||||
public void deleteExpiredCodes() {
|
public void deleteExpiredCodes() {
|
||||||
remove(new QueryWrapper<RegistrationCode>().lt("expiry_time", LocalDateTime.now()));
|
remove(new QueryWrapper<RegistrationCode>().lt("expiry_time", LocalDateTime.now().toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.test.bijihoudaun.service.impl;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.test.bijihoudaun.common.exception.RegistrationException;
|
||||||
import com.test.bijihoudaun.entity.User;
|
import com.test.bijihoudaun.entity.User;
|
||||||
import com.test.bijihoudaun.mapper.UserMapper;
|
import com.test.bijihoudaun.mapper.UserMapper;
|
||||||
import com.test.bijihoudaun.service.UserService;
|
import com.test.bijihoudaun.service.UserService;
|
||||||
@@ -40,18 +41,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||||||
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
queryWrapper.eq(User::getUsername, username);
|
queryWrapper.eq(User::getUsername, username);
|
||||||
if (this.count(queryWrapper) > 0) {
|
if (this.count(queryWrapper) > 0) {
|
||||||
return null;
|
throw new RegistrationException("用户名已存在");
|
||||||
}
|
}
|
||||||
queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper = new LambdaQueryWrapper<>();
|
||||||
queryWrapper.eq(User::getEmail, email);
|
queryWrapper.eq(User::getEmail, email);
|
||||||
if (this.count(queryWrapper) > 0) {
|
if (this.count(queryWrapper) > 0) {
|
||||||
return null;
|
throw new RegistrationException("电子邮箱已被注册");
|
||||||
}
|
}
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setUsername(username);
|
user.setUsername(username);
|
||||||
user.setPassword(encrypt);
|
user.setPassword(encrypt);
|
||||||
user.setEmail(email);
|
user.setEmail(email);
|
||||||
user.setCreatedAt(new Date());
|
// user.setCreatedAt(new Date()); // Let the database handle the default value
|
||||||
userMapper.insert(user);
|
userMapper.insert(user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
mydatabase.db
BIN
mydatabase.db
Binary file not shown.
Reference in New Issue
Block a user