add 完成 国际化 功能

This commit is contained in:
疯狂的狮子li
2022-02-21 13:34:58 +08:00
parent 2ccf189139
commit dc6f7cd691
20 changed files with 640 additions and 430 deletions

View File

@@ -22,16 +22,16 @@ public class LoginBody {
/**
* 用户名
*/
@NotBlank(message = "用户名不能为空")
@Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "账户长度必须在2到20个字符之间")
@NotBlank(message = "{user.username.not.blank}")
@Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "{user.username.length.valid}")
@ApiModelProperty(value = "用户名")
private String username;
/**
* 用户密码
*/
@NotBlank(message = "密码不能为空")
@Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "密码长度必须在5到20个字符之间")
@NotBlank(message = "{user.password.not.blank}")
@Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "{user.password.length.valid}")
@ApiModelProperty(value = "用户密码")
private String password;

View File

@@ -5,9 +5,10 @@ import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.enums.UserType;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.utils.RedisUtils;
@@ -44,8 +45,8 @@ public class SysLoginService {
userInfo = remoteUserService.getUserInfo(username);
if (ObjectUtil.isNull(userInfo)) {
recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists", username));
throw new UserException("user.not.exists", username);
}
} catch (Exception e) {
recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage());
@@ -56,9 +57,8 @@ public class SysLoginService {
Integer errorNumber = RedisUtils.getCacheObject(CacheConstants.LOGIN_ERROR + username);
// 锁定时间内登录 则踢出
if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) {
String msg = "密码错误次数过多,帐户锁定" + CacheConstants.LOGIN_ERROR_LIMIT_TIME + "分钟";
recordLogininfor(username, Constants.LOGIN_FAIL, msg);
throw new ServiceException(msg, null);
recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME));
throw new UserException("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME);
}
if (!BCrypt.checkpw(password, userInfo.getPassword())) {
@@ -66,26 +66,24 @@ public class SysLoginService {
errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
// 达到规定错误次数 则锁定登录
if (errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) {
String msg = "密码错误次数过多,帐户锁定" + CacheConstants.LOGIN_ERROR_LIMIT_TIME + "分钟";
RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber, CacheConstants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
recordLogininfor(username, Constants.LOGIN_FAIL, msg);
throw new ServiceException(msg, null);
recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME));
throw new UserException("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME);
} else {
// 未达到规定错误次数 则递增
String msg = "密码输入错误" + errorNumber + "";
RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber);
recordLogininfor(username, Constants.LOGIN_FAIL, msg);
throw new ServiceException(msg, null);
recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", errorNumber));
throw new UserException("user.password.retry.limit.count", errorNumber);
}
}
// 登录成功 清空错误次数
RedisUtils.deleteObject(CacheConstants.LOGIN_ERROR + username);
recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
return userInfo;
}
public void logout(String loginName) {
recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"));
}
/**
@@ -97,9 +95,6 @@ public class SysLoginService {
// 校验用户类型是否存在
String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
if (UserConstants.NOT_UNIQUE.equals(remoteUserService.checkUserNameUnique(username))) {
throw new ServiceException("保存用户 " + username + " 失败,注册账号已存在");
}
// 注册用户信息
SysUser sysUser = new SysUser();
sysUser.setUserName(username);
@@ -108,9 +103,9 @@ public class SysLoginService {
sysUser.setUserType(userType);
boolean regFlag = remoteUserService.registerUserInfo(sysUser);
if (!regFlag) {
throw new ServiceException("注册失败,请联系系统管理人员");
throw new UserException("user.register.error");
}
recordLogininfor(username, Constants.REGISTER, "注册成功");
recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"));
}
/**