refactor: 用 @Email 和 @Mobile 注解替换了部分验证,提高了代码可读性,修改了多处错误提示信息,使其更加友好

This commit is contained in:
2025-03-27 21:21:53 +08:00
parent c130f9c0bb
commit 19639c946a
26 changed files with 60 additions and 65 deletions

View File

@@ -67,7 +67,7 @@ public abstract class AbstractLoginHandler<T extends LoginReq> implements LoginH
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
protected static final String CAPTCHA_EXPIRED = "验证码已失效";
protected static final String CAPTCHA_ERROR = "验证码错误";
protected static final String CAPTCHA_ERROR = "验证码不正确";
protected static final String CLIENT_ID = "clientId";
@Override

View File

@@ -63,7 +63,7 @@ public class AccountLoginHandler extends AbstractLoginHandler<AccountLoginReq> {
boolean isError = ObjectUtil.isNull(user) || !passwordEncoder.matches(rawPassword, user.getPassword());
// 检查账号锁定状态
this.checkUserLocked(req.getUsername(), request, isError);
ValidationUtils.throwIf(isError, "用户名或密码错误");
ValidationUtils.throwIf(isError, "用户名或密码不正确");
// 检查用户状态
super.checkUserStatus(user);
// 执行认证
@@ -97,7 +97,7 @@ public class AccountLoginHandler extends AbstractLoginHandler<AccountLoginReq> {
*
* @param username 用户名
* @param request 请求对象
* @param isError 是否登录错误
* @param isError 是否登录失败
*/
private void checkUserLocked(String username, HttpServletRequest request, boolean isError) {
// 不锁定

View File

@@ -16,10 +16,9 @@
package top.continew.admin.auth.model.req;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@@ -43,7 +42,7 @@ public class EmailLoginReq extends LoginReq {
*/
@Schema(description = "邮箱", example = "123456789@qq.com")
@NotBlank(message = "邮箱不能为空")
@Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误")
@Email(message = "邮箱格式不正确")
private String email;
/**
@@ -51,6 +50,6 @@ public class EmailLoginReq extends LoginReq {
*/
@Schema(description = "验证码", example = "888888")
@NotBlank(message = "验证码不能为空")
@Length(max = 6, message = "验证码非法")
@Length(max = 6, message = "验证码无效")
private String captcha;
}

View File

@@ -57,6 +57,6 @@ public class LoginReq implements Serializable {
* 认证类型
*/
@Schema(description = "认证类型", example = "ACCOUNT")
@NotNull(message = "认证类型非法")
@NotNull(message = "认证类型无效")
private AuthTypeEnum authType;
}

View File

@@ -16,12 +16,11 @@
package top.continew.admin.auth.model.req;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import top.continew.starter.core.validation.constraints.Mobile;
import java.io.Serial;
@@ -43,7 +42,7 @@ public class PhoneLoginReq extends LoginReq {
*/
@Schema(description = "手机号", example = "13811111111")
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = RegexPool.MOBILE, message = "手机号格式错误")
@Mobile
private String phone;
/**
@@ -51,6 +50,6 @@ public class PhoneLoginReq extends LoginReq {
*/
@Schema(description = "验证码", example = "888888")
@NotBlank(message = "验证码不能为空")
@Length(max = 6, message = "验证码非法")
@Length(max = 6, message = "验证码无效")
private String captcha;
}

View File

@@ -47,7 +47,7 @@ public enum PasswordPolicyEnum {
/**
* 密码错误锁定阈值
*/
PASSWORD_ERROR_LOCK_COUNT("密码错误锁定阈值取值范围为 %d-%d", SysConstants.NO, 10, "密码错误已达 %d 次,账号锁定 %d 分钟"),
PASSWORD_ERROR_LOCK_COUNT("密码错误锁定阈值取值范围为 %d-%d", SysConstants.NO, 10, "密码不正确已达 %d 次,账号锁定 %d 分钟"),
/**
* 账号锁定时长(分钟)

View File

@@ -51,6 +51,6 @@ public class OptionQuery implements Serializable {
* 类别
*/
@Schema(description = "类别", example = "SITE")
@EnumValue(value = OptionCategoryEnum.class, message = "类别非法")
@EnumValue(value = OptionCategoryEnum.class, message = "类别无效")
private String category;
}

View File

@@ -45,7 +45,7 @@ public class MenuReq implements Serializable {
* 类型
*/
@Schema(description = "类型", example = "2")
@NotNull(message = "类型非法")
@NotNull(message = "类型无效")
private MenuTypeEnum type;
/**
@@ -133,6 +133,6 @@ public class MenuReq implements Serializable {
* 状态
*/
@Schema(description = "状态", example = "1")
@NotNull(message = "状态非法")
@NotNull(message = "状态无效")
private DisEnableStatusEnum status;
}

View File

@@ -59,6 +59,6 @@ public class MessageReq implements Serializable {
* 类型
*/
@Schema(description = "类型1系统消息", example = "1")
@NotNull(message = "类型非法")
@NotNull(message = "类型无效")
private MessageTypeEnum type;
}

View File

@@ -64,7 +64,7 @@ public class StorageReq implements Serializable {
* 类型
*/
@Schema(description = "类型", example = "2")
@NotNull(message = "类型非法")
@NotNull(message = "类型无效")
private StorageTypeEnum type;
/**

View File

@@ -52,6 +52,6 @@ public class UserBasicInfoUpdateReq implements Serializable {
* 性别
*/
@Schema(description = "性别", example = "1")
@NotNull(message = "性别非法")
@NotNull(message = "性别无效")
private GenderEnum gender;
}

View File

@@ -16,10 +16,9 @@
package top.continew.admin.system.model.req.user;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@@ -44,7 +43,7 @@ public class UserEmailUpdateRequest implements Serializable {
*/
@Schema(description = "新邮箱", example = "123456789@qq.com")
@NotBlank(message = "新邮箱不能为空")
@Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误")
@Email(message = "邮箱格式不正确")
private String email;
/**
@@ -52,7 +51,7 @@ public class UserEmailUpdateRequest implements Serializable {
*/
@Schema(description = "验证码", example = "888888")
@NotBlank(message = "验证码不能为空")
@Length(max = 6, message = "验证码非法")
@Length(max = 6, message = "验证码无效")
private String captcha;
/**

View File

@@ -16,13 +16,14 @@
package top.continew.admin.system.model.req.user;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import top.continew.admin.common.constant.RegexConstants;
import top.continew.starter.core.validation.constraints.Mobile;
import top.continew.starter.extension.crud.validation.CrudValidationGroup;
import java.io.Serial;
@@ -81,14 +82,14 @@ public class UserImportRowReq implements Serializable {
/**
* 邮箱
*/
@Pattern(regexp = "^$|" + RegexPool.EMAIL, message = "邮箱格式错误")
@Length(max = 255, message = "邮箱长度不能超过 {max} 个字符")
@Email(message = "邮箱格式不正确")
private String email;
/**
* 手机号码
*/
@Pattern(regexp = "^$|" + RegexPool.MOBILE, message = "手机号码格式错误")
@Mobile
private String phone;
/**

View File

@@ -16,12 +16,11 @@
package top.continew.admin.system.model.req.user;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import top.continew.starter.core.validation.constraints.Mobile;
import java.io.Serial;
import java.io.Serializable;
@@ -44,7 +43,7 @@ public class UserPhoneUpdateReq implements Serializable {
*/
@Schema(description = "新手机号", example = "13811111111")
@NotBlank(message = "新手机号不能为空")
@Pattern(regexp = RegexPool.MOBILE, message = "手机号格式错误")
@Mobile(message = "手机号格式不正确")
private String phone;
/**
@@ -52,7 +51,7 @@ public class UserPhoneUpdateReq implements Serializable {
*/
@Schema(description = "验证码", example = "888888")
@NotBlank(message = "验证码不能为空")
@Length(max = 6, message = "验证码非法")
@Length(max = 6, message = "验证码无效")
private String captcha;
/**

View File

@@ -16,7 +16,6 @@
package top.continew.admin.system.model.req.user;
import cn.hutool.core.lang.RegexPool;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.Data;
@@ -24,6 +23,7 @@ import org.hibernate.validator.constraints.Length;
import top.continew.admin.common.constant.RegexConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.starter.core.validation.constraints.Mobile;
import top.continew.starter.extension.crud.validation.CrudValidationGroup;
import java.io.Serial;
@@ -70,23 +70,22 @@ public class UserReq implements Serializable {
* 邮箱
*/
@Schema(description = "邮箱", example = "123456789@qq.com")
@Pattern(regexp = "^$|" + RegexPool.EMAIL, message = "邮箱格式错误")
@Length(max = 255, message = "邮箱长度不能超过 {max} 个字符")
@Email
@Email(message = "邮箱格式不正确")
private String email;
/**
* 手机号
* 手机号
*/
@Schema(description = "手机号", example = "13811111111")
@Pattern(regexp = "^$|" + RegexPool.MOBILE, message = "手机号码格式错误")
@Schema(description = "手机号", example = "13811111111")
@Mobile
private String phone;
/**
* 性别
*/
@Schema(description = "性别", example = "1")
@NotNull(message = "性别非法")
@NotNull(message = "性别无效")
private GenderEnum gender;
/**

View File

@@ -134,7 +134,7 @@ public class OptionServiceImpl implements OptionService {
.one();
CheckUtils.throwIfNull(option, "参数 [{}] 不存在", code);
value = StrUtil.nullToDefault(option.getValue(), option.getDefaultValue());
CheckUtils.throwIfBlank(value, "参数 [{}] 数据错误", code);
CheckUtils.throwIfBlank(value, "参数 [{}] 数据不正确", code);
RedisUtils.set(CacheConstants.OPTION_KEY_PREFIX + code, value);
return mapper.apply(value);
}

View File

@@ -164,7 +164,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
public void load(StorageReq req) {
CopyOnWriteArrayList<FileStorage> fileStorageList = fileStorageService.getFileStorageList();
String domain = req.getDomain();
ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式错误");
ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式不正确");
String bucketName = req.getBucketName();
StorageTypeEnum type = req.getType();
if (StorageTypeEnum.LOCAL.equals(type)) {

View File

@@ -253,11 +253,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
}
// 总计行数
userImportResp.setTotalRows(importRowList.size());
CheckUtils.throwIfEmpty(importRowList, "数据文件格式错误");
CheckUtils.throwIfEmpty(importRowList, "数据文件格式不正确");
// 有效行数:过滤无效数据
List<UserImportRowReq> validRowList = this.filterImportData(importRowList);
userImportResp.setValidRows(validRowList.size());
CheckUtils.throwIfEmpty(validRowList, "数据文件格式错误");
CheckUtils.throwIfEmpty(validRowList, "数据文件格式不正确");
// 检测表格内数据是否合法
Set<String> seenEmails = new HashSet<>();
@@ -271,14 +271,14 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
.anyMatch(phone -> phone != null && !seenPhones.add(phone));
CheckUtils.throwIf(hasDuplicatePhone, "存在重复手机,请检测数据");
// 校验是否存在错误角色
// 校验是否存在无效角色
List<String> roleNames = validRowList.stream().map(UserImportRowReq::getRoleName).distinct().toList();
int existRoleCount = roleService.countByNames(roleNames);
CheckUtils.throwIf(existRoleCount < roleNames.size(), "存在错误角色,请检查数据");
// 校验是否存在错误部门
CheckUtils.throwIf(existRoleCount < roleNames.size(), "存在无效角色,请检查数据");
// 校验是否存在无效部门
List<String> deptNames = validRowList.stream().map(UserImportRowReq::getDeptName).distinct().toList();
int existDeptCount = deptService.countByNames(deptNames);
CheckUtils.throwIf(existDeptCount < deptNames.size(), "存在错误部门,请检查数据");
CheckUtils.throwIf(existDeptCount < deptNames.size(), "存在无效部门,请检查数据");
// 查询重复用户
userImportResp
@@ -425,7 +425,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
UserDO user = super.getById(id);
String password = user.getPassword();
if (StrUtil.isNotBlank(password)) {
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, password), "当前密码错误");
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, password), "当前密码不正确");
}
// 校验密码合法性
int passwordRepetitionTimes = this.checkPassword(newPassword, user);
@@ -444,7 +444,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
@Override
public void updatePhone(String newPhone, String oldPassword, Long id) {
UserDO user = super.getById(id);
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, user.getPassword()), "当前密码错误");
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, user.getPassword()), "当前密码不正确");
CheckUtils.throwIf(this.isPhoneExists(newPhone, id), "手机号已绑定其他账号,请更换其他手机号");
CheckUtils.throwIfEqual(newPhone, user.getPhone(), "新手机号不能与当前手机号相同");
// 更新手机号
@@ -454,7 +454,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
@Override
public void updateEmail(String newEmail, String oldPassword, Long id) {
UserDO user = super.getById(id);
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, user.getPassword()), "当前密码错误");
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, user.getPassword()), "当前密码不正确");
CheckUtils.throwIf(this.isEmailExists(newEmail, id), "邮箱已绑定其他账号,请更换其他邮箱");
CheckUtils.throwIfEqual(newEmail, user.getEmail(), "新邮箱不能与当前邮箱相同");
// 更新邮箱