add 完成 国际化 功能

2.X
疯狂的狮子li 3 years ago
parent 2ccf189139
commit dc6f7cd691

@ -56,7 +56,7 @@
| 工具类框架 | Hutool、Lombok | [Hutool文档](https://www.hutool.cn/docs/) | 减少代码冗余 增加安全性 |
| 代码生成器 | 适配MP、Knife4j规范化代码 | [Hutool文档](https://www.hutool.cn/docs/) | 一键生成前后端代码 |
| 部署方式 | Docker | [Docker文档](https://docs.docker.com/) | 容器编排 一键部署业务集群 |
| 国际化(未完成) | SpringMessage | [SpringMVC文档](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc) | Spring标准国际化方案 |
| 国际化 | SpringMessage | [SpringMVC文档](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc) | Spring标准国际化方案 |
## 软件架构图

@ -55,6 +55,10 @@ spring:
pathmatch:
# 适配 boot 2.6 路由与 springfox 兼容
matching-strategy: ANT_PATH_MATCHER
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
servlet:
multipart:
# 整个请求大小限制

@ -26,11 +26,4 @@ public interface RemoteUserService {
*/
Boolean registerUserInfo(SysUser sysUser);
/**
*
*
* @param username
* @return
*/
String checkUserNameUnique(String username);
}

@ -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;

@ -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"));
}
/**

@ -3,11 +3,15 @@ package com.ruoyi.common.core.exception;
/**
*
*
* @author ruoyi
* @author Lion Li
*/
public class CaptchaException extends RuntimeException {
private static final long serialVersionUID = 1L;
public CaptchaException() {
super("user.jcaptcha.error");
}
public CaptchaException(String msg) {
super(msg);
}

@ -42,6 +42,7 @@ public final class ServiceException extends RuntimeException {
return detailMessage;
}
@Override
public String getMessage() {
return message;
}

@ -1,5 +1,7 @@
package com.ruoyi.common.core.exception.base;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -49,4 +51,16 @@ public class BaseException extends RuntimeException {
this(null, null, null, defaultMessage);
}
@Override
public String getMessage() {
String message = null;
if (!StringUtils.isEmpty(code)) {
message = MessageUtils.message(code, args);
}
if (message == null) {
message = defaultMessage;
}
return message;
}
}

@ -5,12 +5,12 @@ import com.ruoyi.common.core.exception.base.BaseException;
/**
*
*
* @author ruoyi
* @author Lion Li
*/
public class UserException extends BaseException {
private static final long serialVersionUID = 1L;
public UserException(String code, Object[] args) {
public UserException(String code, Object... args) {
super("user", code, args, null);
}
}

@ -0,0 +1,28 @@
package com.ruoyi.common.core.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* i18n
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageUtils {
private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);
/**
* spring messageSource
*
* @param code
* @param args
* @return
*/
public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
}
}

@ -0,0 +1,21 @@
package com.ruoyi.common.web.config;
import com.ruoyi.common.web.core.I18nLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
/**
*
*
* @author Lion Li
*/
@Configuration
public class I18nConfig {
@Bean
public LocaleResolver localeResolver() {
return new I18nLocaleResolver();
}
}

@ -0,0 +1,31 @@
package com.ruoyi.common.web.core;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
*
*
* @author Lion Li
*/
public class I18nLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String language = httpServletRequest.getHeader("content-language");
Locale locale = Locale.getDefault();
if (language != null && language.length() > 0) {
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}

@ -1 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ruoyi.common.web.config.I18nConfig

@ -0,0 +1,39 @@
#错误消息
not.null=* 必须填写
user.jcaptcha.not.blank=验证码不能为空
user.jcaptcha.error=验证码错误
user.jcaptcha.expire=验证码已失效
user.not.exists=对不起, 您的账号:{0} 不存在.
user.password.not.match=用户不存在/密码错误
user.password.retry.limit.count=密码输入错误{0}次
user.password.retry.limit.exceed=密码错误次数过多,帐户锁定{0}分钟
user.password.delete=对不起,您的账号:{0} 已被删除
user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员
role.blocked=角色已封禁,请联系管理员
user.logout.success=退出成功
length.not.valid=长度必须在{min}到{max}个字符之间
user.username.not.blank=用户名不能为空
user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成且必须以非数字开头
user.username.length.valid=账户长度必须在{min}到{max}个字符之间
user.password.not.blank=用户密码不能为空
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
user.password.not.valid=* 5-50个字符
user.email.not.valid=邮箱格式错误
user.mobile.phone.number.not.valid=手机号格式错误
user.login.success=登录成功
user.register.success=注册成功
user.register.save.error=保存用户 {0} 失败,注册账号已存在
user.register.error=注册失败,请联系系统管理人员
user.notfound=请重新登录
user.forcelogout=管理员强制退出,请重新登录
user.unknown.error=未知错误,请重新登录
##文件上传消息
upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB
upload.filename.exceed.length=上传的文件名最长{0}个字符
##权限
no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]

@ -0,0 +1,39 @@
#错误消息
not.null=* Required fill in
user.jcaptcha.not.blank=Captcha cannot be blank
user.jcaptcha.error=Captcha error
user.jcaptcha.expire=Captcha invalid
user.not.exists=Sorry, your account: {0} does not exist
user.password.not.match=User does not exist/Password error
user.password.retry.limit.count=Password input error {0} times
user.password.retry.limit.exceed=Too many password errors, account locked for {0} minutes
user.password.delete=Sorry, your account{0} has been deleted
user.blocked=Sorry, your account: {0} has been disabled. Please contact the administrator
role.blocked=Role disabledplease contact administrators
user.logout.success=Exit successful
length.not.valid=The length must be between {min} and {max} characters
user.username.not.blank=Username cannot be blank
user.username.not.valid=* 2 to 20 chinese characters, letters, numbers or underscores, and must start with a non number
user.username.length.valid=Account length must be between {min} and {max} characters
user.password.not.blank=Password cannot be empty
user.password.length.valid=Password length must be between {min} and {max} characters
user.password.not.valid=* 5-50 characters
user.email.not.valid=Mailbox format error
user.mobile.phone.number.not.valid=Phone number format error
user.login.success=Login successful
user.register.success=Register successful
user.register.save.error=Failed to save user {0}, The registered account already exists
user.register.error=Register failed, please contact system administrator
user.notfound=Please login again
user.forcelogout=The administrator is forced to exitplease login again
user.unknown.error=Unknown error, please login again
##文件上传消息
upload.exceed.maxSize=The uploaded file size exceeds the limit file size<br/>the maximum allowed file size is{0}MB
upload.filename.exceed.length=The maximum length of uploaded file name is {0} characters
##权限
no.permission=You do not have permission to the dataplease contact your administrator to add permissions [{0}]
no.create.permission=You do not have permission to create dataplease contact your administrator to add permissions [{0}]
no.update.permission=You do not have permission to modify dataplease contact your administrator to add permissions [{0}]
no.delete.permission=You do not have permission to delete dataplease contact your administrator to add permissions [{0}]
no.export.permission=You do not have permission to export dataplease contact your administrator to add permissions [{0}]
no.view.permission=You do not have permission to view dataplease contact your administrator to add permissions [{0}]

@ -0,0 +1,39 @@
#错误消息
not.null=* 必须填写
user.jcaptcha.not.blank=验证码不能为空
user.jcaptcha.error=验证码错误
user.jcaptcha.expire=验证码已失效
user.not.exists=对不起, 您的账号:{0} 不存在.
user.password.not.match=用户不存在/密码错误
user.password.retry.limit.count=密码输入错误{0}次
user.password.retry.limit.exceed=密码错误次数过多,帐户锁定{0}分钟
user.password.delete=对不起,您的账号:{0} 已被删除
user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员
role.blocked=角色已封禁,请联系管理员
user.logout.success=退出成功
length.not.valid=长度必须在{min}到{max}个字符之间
user.username.not.blank=用户名不能为空
user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成且必须以非数字开头
user.username.length.valid=账户长度必须在{min}到{max}个字符之间
user.password.not.blank=用户密码不能为空
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
user.password.not.valid=* 5-50个字符
user.email.not.valid=邮箱格式错误
user.mobile.phone.number.not.valid=手机号格式错误
user.login.success=登录成功
user.register.success=注册成功
user.register.save.error=保存用户 {0} 失败,注册账号已存在
user.register.error=注册失败,请联系系统管理人员
user.notfound=请重新登录
user.forcelogout=管理员强制退出,请重新登录
user.unknown.error=未知错误,请重新登录
##文件上传消息
upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB
upload.filename.exceed.length=上传的文件名最长{0}个字符
##权限
no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]

@ -7,6 +7,7 @@ import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.exception.user.CaptchaExpireException;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.reflect.ReflectUtils;
@ -85,17 +86,17 @@ public class ValidateCodeServiceImpl implements ValidateCodeService {
@Override
public void checkCaptcha(String code, String uuid) throws CaptchaException {
if (StringUtils.isEmpty(code)) {
throw new CaptchaException("验证码不能为空");
throw new CaptchaException("user.jcaptcha.not.blank");
}
if (StringUtils.isEmpty(uuid)) {
throw new CaptchaException("验证码已失效");
throw new CaptchaExpireException();
}
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String captcha = RedisUtils.getCacheObject(verifyKey);
RedisUtils.deleteObject(verifyKey);
if (!code.equalsIgnoreCase(captcha)) {
throw new CaptchaException("验证码错误");
throw new CaptchaException();
}
}
}

@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
@ -37,13 +38,13 @@ public class RemoteUserServiceImpl implements RemoteUserService {
public LoginUser getUserInfo(String username) {
SysUser sysUser = userService.selectUserByUserName(username);
if (ObjectUtil.isNull(sysUser)) {
throw new ServiceException("用户名或密码错误");
throw new UserException("user.not.exists", username);
}
if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) {
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
throw new UserException("user.password.delete", username);
}
if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
throw new UserException("user.blocked", username);
}
// 角色集合
Set<String> rolePermission = permissionService.getRolePermission(sysUser.getUserId());
@ -70,13 +71,9 @@ public class RemoteUserServiceImpl implements RemoteUserService {
throw new ServiceException("当前系统没有开启注册功能");
}
if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
throw new ServiceException("保存用户'" + username + "'失败,注册账号已存在");
throw new UserException("user.register.save.error", username);
}
return userService.registerUser(sysUser);
}
@Override
public String checkUserNameUnique(String username) {
return userService.checkUserNameUnique(username);
}
}

@ -12,6 +12,8 @@ let downloadLoadingInstance;
let isReloginShow;
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 对应国际化资源文件后缀
axios.defaults.headers['Content-Language'] = 'zh_CN'
// 创建axios实例
const service = axios.create({
// axios中请求配置有baseURL选项表示请求URL公共部分

@ -86,7 +86,8 @@ export default {
password: "",
confirmPassword: "",
code: "",
uuid: ""
uuid: "",
user_type: "sys_user"
},
registerRules: {
username: [

Loading…
Cancel
Save