1. 错误码支持国际化

This commit is contained in:
黄昕 2024-03-24 09:56:49 +08:00
parent b9e4be0866
commit 76df3543e1
9 changed files with 197 additions and 111 deletions

View File

@ -1,5 +1,6 @@
package com.cf.cs.authentication.interceptor; package com.cf.cs.authentication.interceptor;
import com.cf.cs.base.common.ConstValue;
import com.cf.cs.base.misc.ApiContextUtils; import com.cf.cs.base.misc.ApiContextUtils;
import com.cf.cs.base.misc.HelperUtils; import com.cf.cs.base.misc.HelperUtils;
import com.cf.cs.protocol.misc.ProtocolJsonUtils; import com.cf.cs.protocol.misc.ProtocolJsonUtils;
@ -74,6 +75,8 @@ public class RequestBodyCacheWrapper extends HttpServletRequestWrapper {
} catch (Exception e) { } catch (Exception e) {
body = ""; body = "";
} finally { } finally {
String language = request.getHeader(ConstValue.LANGUAGE_HEAD);
ApiContextUtils.setRequestLocal(language);
ApiContextUtils.setRequestInfo(body, reqTimeStamp); ApiContextUtils.setRequestInfo(body, reqTimeStamp);
} }
} }

View File

@ -20,9 +20,21 @@ public class ConstValue {
* The constant STRING_HTTP_AUTH_HEAD. * The constant STRING_HTTP_AUTH_HEAD.
*/ */
public static final String STRING_HTTP_AUTH_HEAD = "Bearer "; public static final String STRING_HTTP_AUTH_HEAD = "Bearer ";
/**
* The constant LANGUAGE_HEAD.
*/
public static final String LANGUAGE_HEAD = "language";
/**
* The constant MS_OF_SECONDS.
*/
public static final long MS_OF_SECONDS = 1000L; public static final long MS_OF_SECONDS = 1000L;
public static final long SECOND_OF_MINUTE = 60; public static final long SECOND_OF_MINUTE = 60;
/**
* The constant MAX_IPV4_LEN.
*/
public static final int MAX_IPV4_LEN = 15; public static final int MAX_IPV4_LEN = 15;
/** /**

View File

@ -1,5 +1,7 @@
package com.cf.cs.base.common; package com.cf.cs.base.common;
import com.cf.cs.base.misc.ApiContextUtils;
import com.cf.cs.base.misc.MessageUtil;
import com.houkunlin.system.dict.starter.DictEnum; import com.houkunlin.system.dict.starter.DictEnum;
import com.houkunlin.system.dict.starter.json.DictType; import com.houkunlin.system.dict.starter.json.DictType;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -292,6 +294,6 @@ public enum ErrorCode implements BaseEnum, DictEnum<Integer> {
@Override @Override
public String getDescription() { public String getDescription() {
return this.errMsg; return MessageUtil.get(getStringValue(), ApiContextUtils.getLanguare());
} }
} }

View File

@ -23,7 +23,7 @@ public class LocaleConfig {
Locale.setDefault(Locale.CHINA); Locale.setDefault(Locale.CHINA);
ResourceBundleMessageSource source = new ResourceBundleMessageSource(); ResourceBundleMessageSource source = new ResourceBundleMessageSource();
//设置国际化文件存储路径和名称 i18n目录messages文件名 //设置国际化文件存储路径和名称 i18n目录messages文件名
source.setBasenames("i18n/messages"); source.setBasenames("i18n/message", "i18n/errorMessage");
//设置根据key如果没有获取到对应的文本信息,则返回key作为信息 //设置根据key如果没有获取到对应的文本信息,则返回key作为信息
source.setUseCodeAsDefaultMessage(true); source.setUseCodeAsDefaultMessage(true);
//设置字符编码 //设置字符编码

View File

@ -0,0 +1,69 @@
package com.cf.cs.base.misc;
import io.micrometer.common.util.StringUtils;
import org.springframework.context.MessageSource;
import java.util.Locale;
/**
* The type Message util.
*
* @author xajhuang @163.com
*/
public class MessageUtil {
private MessageUtil() {
throw new AssertionError("Instantiating utility class.");
}
/**
* Get string.
*
* @param key the key
* @param language the language
* @return the string
*/
public static String get(String key, String language) {
if (!StringUtils.isEmpty(language)) {
String[] arrs = language.split("_");
if (arrs.length == 2) {
return get(key, new Locale(arrs[0], arrs[1]));
}
}
//使用默认的国际化语言
return get(key, Locale.getDefault());
}
/**
* Get string.
*
* @param key the key
* @param params the params
* @param language the language
* @return the string
*/
public static String get(String key, Object[] params, String language) {
if (!StringUtils.isEmpty(language)) {
String[] arrs = language.split("_");
if (arrs.length == 2) {
return get(key, params, new Locale(arrs[0], arrs[1]));
}
}
return get(key, params, Locale.getDefault());
}
private static String get(String key, Locale language) {
return get(key, new String[0], language);
}
private static String get(String key, Object[] params, Locale language) {
return getInstance().getMessage(key, params, language);
}
private static MessageSource getInstance() {
return Lazy.MESSAGE_SOURCE;
}
private static class Lazy {
private static final MessageSource MESSAGE_SOURCE = SpringBootBeanUtils.getBean(MessageSource.class);
}
}

View File

@ -0,0 +1,54 @@
ERR_OK=Successful
ERR_PASSWORD=Incorrect password
ERR_USERNOTFOUND=User does not exist
ERR_PASSWORDMORE=The number of consecutive incorrect passwords reaches the upper limit
ERR_USERLOCK=Password error reaches the upper limit, The user is locked
ERR_PASSWORD_EXPIRED=The password has expired
ERR_ACCOUNT=The user account is abnormal
ERR_USEREXIST=The user already exists
ERR_PASSWORDSIMPLE=The user password strength does not meet the requirement. ERR_INPUTFORMAT=The input information format is incorrect
ERR_INPUTMISS=No necessary input information
ERR_PERMISSION=Operator permission is insufficient
ERR_REQTIMEOUT=Request timeout
ERR_PARAMS=Parameter error
ERR_SYSTEMEXCEPTION=System exception
ERR_UNKNOWNCMD=unknown command
ERR_LOGOUT=User not logged in
ERR_TOKENTIMEOUT=Token timeout
ERR_TOKENNOTFOUND=Illegal Token
ERR_TOKEN_KEY=Token key error
ERR_MISSAUTHHEAD=Http request missing authentication header
ERR_NOSUCHITEM=No such content
ERR_ITEMEXISTS=The content already exists
ERR_PARAMEXCEPTION=Parameter exception
ERR_DEVICELOCKED=Device locked
ERR_VERSION=Protocol version incompatible, Please upgrade the system
ERR_NOSUCHTYPE=No device of this type exists
ERR_REMOVEMORE=Disable deleting multiple devices at the same time
ERR_TASKRUNNING=Similar tasks are running
ERR_UNSUPPORT=Unsupported operations
ERR_INTERRUPT=Operation interruption
ERR_CALLDEVICE=Failed to call the device
ERR_NOSUCHTASK=No task
ERR_TASKNOTRUNNING=No task is running
ERR_REQUESTTIMEOUT=Request timeout
ERR_UNABLEDISPOSEIP=Unable to handle the IP
ERR_DATABASE=Failed to operate the database
ERR_UNTRUSTHOST=Unauthorized client
ERR_UNTRUSTTOKEN=Unauthorized Token
ERR_UNKNOWNINTERFACE=Interface not provided
ERR_DECRYPT_BASE64=BASE64 decryption failure
ERR_ENCRYPT_BASE64=BASE64 encryption failure
ERR_DECRYPT_AES128=AES128 decryption failure
ERR_ENCRYPT_AES128=AES128 encryption failure
ERR_DECRYPT_3DES=3DES decryption failure
ERR_ENCRYPT_3DES=3DES Encryption failure
ERR_DECRYPT_UNKNOWN=Unsupported decryption algorithm
ERR_ENCRYPT_UNKNOWN=unsupported encryption algorithm
ERR_JSON_ENCODE=Json Sequence number error
ERR_JSON_DECODE=Json deserialization error
ERR_ENCRYPT_AES256=AES256 Encryption failure
ERR_DECRYPT_AES256=AES256 decryption failure
ERR_CRYPTO_KEY=wrong secret key
ERR_USER_ROLE_NOTEXISTS=The user role does not exist
ERR_RESOURCE_USED=Resource used

View File

@ -0,0 +1,55 @@
ERR_OK=\u6210\u529F
ERR_PASSWORD=\u5BC6\u7801\u9519\u8BEF
ERR_USERNOTFOUND=\u7528\u6237\u4E0D\u5B58\u5728
ERR_PASSWORDMORE=\u8FDE\u7EED\u5BC6\u7801\u9519\u8BEF\u8FBE\u4E0A\u9650\uFF0C\u518D\u6B21\u8F93\u5165\u9519\u8BEF\u5C06\u9501\u5B9A\u7528\u6237
ERR_USERLOCK=\u5BC6\u7801\u9519\u8BEF\u8FBE\u4E0A\u9650\uFF0C\u7528\u6237\u88AB\u9501\u5B9A
ERR_PASSWORD_EXPIRED=\u5BC6\u7801\u5DF2\u7ECF\u8FC7\u671F
ERR_ACCOUNT=\u7528\u6237\u8D26\u6237\u5F02\u5E38
ERR_USEREXIST=\u8BE5\u7528\u6237\u5DF2\u7ECF\u5B58\u5728
ERR_PASSWORDSIMPLE=\u7528\u6237\u5BC6\u7801\u5F3A\u5EA6\u4E0D\u7B26\u5408\u8981\u6C42
ERR_INPUTFORMAT=\u8F93\u5165\u4FE1\u606F\u683C\u5F0F\u6709\u8BEF
ERR_INPUTMISS=\u7F3A\u5C11\u5FC5\u8981\u8F93\u5165\u4FE1\u606F
ERR_PERMISSION=\u64CD\u4F5C\u5458\u6743\u9650\u4E0D\u8DB3
ERR_REQTIMEOUT=\u8BF7\u6C42\u8D85\u65F6
ERR_PARAMS=\u53C2\u6570\u9519\u8BEF
ERR_SYSTEMEXCEPTION=\u7CFB\u7EDF\u5F02\u5E38
ERR_UNKNOWNCMD=\u672A\u77E5\u547D\u4EE4
ERR_LOGOUT=\u7528\u6237\u672A\u767B\u5F55
ERR_TOKENTIMEOUT=Token\u8D85\u65F6
ERR_TOKENNOTFOUND=\u975E\u6CD5Token
ERR_TOKEN_KEY=Token \u79D8\u94A5\u9519\u8BEF
ERR_MISSAUTHHEAD=Http \u8BF7\u6C42\u7F3A\u5C11\u8BA4\u8BC1\u5934\u90E8
ERR_NOSUCHITEM=\u6CA1\u6709\u8BE5\u5185\u5BB9
ERR_ITEMEXISTS=\u8BE5\u5185\u5BB9\u5DF2\u7ECF\u5B58\u5728
ERR_PARAMEXCEPTION=\u53C2\u6570\u5F02\u5E38
ERR_DEVICELOCKED=\u8BBE\u5907\u5DF2\u9501\u5B9A
ERR_VERSION=\u534F\u8BAE\u7248\u672C\u4E0D\u517C\u5BB9\uFF0C\u8BF7\u5347\u7EA7\u7CFB\u7EDF
ERR_NOSUCHTYPE=\u6CA1\u6709\u8FD9\u4E2A\u7C7B\u578B\u7684\u8BBE\u5907
ERR_REMOVEMORE=\u7981\u6B62\u540C\u65F6\u5220\u9664\u591A\u4E2A\u8BBE\u5907
ERR_TASKRUNNING=\u540C\u7C7B\u4EFB\u52A1\u6B63\u5728\u8FD0\u884C
ERR_UNSUPPORT=\u4E0D\u652F\u6301\u7684\u64CD\u4F5C
ERR_INTERRUPT=\u64CD\u4F5C\u4E2D\u65AD
ERR_CALLDEVICE=\u8C03\u7528\u8BBE\u5907\u5931\u8D25
ERR_NOSUCHTASK=\u6CA1\u6709\u8BE5\u4EFB\u52A1
ERR_TASKNOTRUNNING=\u8BE5\u4EFB\u52A1\u6CA1\u6709\u8FD0\u884C
ERR_REQUESTTIMEOUT=\u8BF7\u6C42\u8D85\u65F6
ERR_UNABLEDISPOSEIP=\u65E0\u6CD5\u5904\u7F6E\u8BE5IP
ERR_DATABASE=\u64CD\u4F5C\u6570\u636E\u5E93\u5931\u8D25
ERR_UNTRUSTHOST=\u672A\u7ECF\u6388\u6743\u7684\u5BA2\u6237\u7AEF
ERR_UNTRUSTTOKEN=\u672A\u7ECF\u6388\u6743\u7684Token
ERR_UNKNOWNINTERFACE=\u672A\u63D0\u4F9B\u8BE5\u63A5\u53E3
ERR_DECRYPT_BASE64=BASE64\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_BASE64=BASE64\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_AES128=AES128\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_AES128=AES128\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_3DES=3DES\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_3DES=3DES\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_UNKNOWN=\u4E0D\u652F\u6301\u7684\u89E3\u5BC6\u7B97\u6CD5
ERR_ENCRYPT_UNKNOWN=\u4E0D\u652F\u6301\u7684\u52A0\u5BC6\u7B97\u6CD5
ERR_JSON_ENCODE=Json \u5E8F\u5217\u53F7\u9519\u8BEF
ERR_JSON_DECODE=Json \u53CD\u5E8F\u5217\u5316\u9519\u8BEF
ERR_ENCRYPT_AES256=AES256\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_AES256=AES256\u89E3\u5BC6\u5931\u8D25
ERR_CRYPTO_KEY=\u9519\u8BEF\u7684\u79D8\u94A5
ERR_USER_ROLE_NOTEXISTS=\u7528\u6237\u89D2\u8272\u4E0D\u5B58\u5728
ERR_RESOURCE_USED=\u8D44\u6E90\u88AB\u5360\u7528

View File

@ -1,54 +0,0 @@
ERR_OK=Successful
ERR_PASSWORD=Incorrect password
ERR_USERNOTFOUND=User does not exist
ERR_PASSWORDMORE=The number of consecutive incorrect passwords reaches the upper limit
ERR_USERLOCK=Password error reaches the upper limit, The user is locked
ERR_PASSWORD_EXPIRED=The password has expired
ERR_ACCOUNT=The user account is abnormal
ERR_USEREXIST=The user already exists
ERR_PASSWORDSIMPLE=The user password strength does not meet the requirement. ERR_INPUTFORMAT=The input information format is incorrect
ERR_INPUTMISS=No necessary input information
ERR_PERMISSION=Operator permission is insufficient
ERR_REQTIMEOUT=Request timeout
ERR_PARAMS=Parameter error
ERR_SYSTEMEXCEPTION=System exception
ERR_UNKNOWNCMD=unknown command
ERR_LOGOUT=User not logged in
ERR_TOKENTIMEOUT=Token timeout
ERR_TOKENNOTFOUND=Illegal Token
ERR_TOKEN_KEY=Token key error
ERR_MISSAUTHHEAD=Http request missing authentication header
ERR_NOSUCHITEM=No such content
ERR_ITEMEXISTS=The content already exists
ERR_PARAMEXCEPTION=Parameter exception
ERR_DEVICELOCKED=Device locked
ERR_VERSION=Protocol version incompatible, Please upgrade the system
ERR_NOSUCHTYPE=No device of this type exists
ERR_REMOVEMORE=Disable deleting multiple devices at the same time
ERR_TASKRUNNING=Similar tasks are running
ERR_UNSUPPORT=Unsupported operations
ERR_INTERRUPT=Operation interruption
ERR_CALLDEVICE=Failed to call the device
ERR_NOSUCHTASK=No task
ERR_TASKNOTRUNNING=No task is running
ERR_REQUESTTIMEOUT=Request timeout
ERR_UNABLEDISPOSEIP=Unable to handle the IP
ERR_DATABASE=Failed to operate the database
ERR_UNTRUSTHOST=Unauthorized client
ERR_UNTRUSTTOKEN=Unauthorized Token
ERR_UNKNOWNINTERFACE=Interface not provided
ERR_DECRYPT_BASE64=BASE64 decryption failure
ERR_ENCRYPT_BASE64=BASE64 encryption failure
ERR_DECRYPT_AES128=AES128 decryption failure
ERR_ENCRYPT_AES128=AES128 encryption failure
ERR_DECRYPT_3DES=3DES decryption failure
ERR_ENCRYPT_3DES=3DES Encryption failure
ERR_DECRYPT_UNKNOWN=Unsupported decryption algorithm
ERR_ENCRYPT_UNKNOWN=unsupported encryption algorithm
ERR_JSON_ENCODE=Json Sequence number error
ERR_JSON_DECODE=Json deserialization error
ERR_ENCRYPT_AES256=AES256 Encryption failure
ERR_DECRYPT_AES256=AES256 decryption failure
ERR_CRYPTO_KEY=wrong secret key
ERR_USER_ROLE_NOTEXISTS=The user role does not exist
ERR_RESOURCE_USED=Resource used

View File

@ -1,55 +0,0 @@
ERR_OK=\u6210\u529F
ERR_PASSWORD=\u5BC6\u7801\u9519\u8BEF
ERR_USERNOTFOUND=\u7528\u6237\u4E0D\u5B58\u5728
ERR_PASSWORDMORE=\u8FDE\u7EED\u5BC6\u7801\u9519\u8BEF\u8FBE\u4E0A\u9650\uFF0C\u518D\u6B21\u8F93\u5165\u9519\u8BEF\u5C06\u9501\u5B9A\u7528\u6237
ERR_USERLOCK=\u5BC6\u7801\u9519\u8BEF\u8FBE\u4E0A\u9650\uFF0C\u7528\u6237\u88AB\u9501\u5B9A
ERR_PASSWORD_EXPIRED=\u5BC6\u7801\u5DF2\u7ECF\u8FC7\u671F
ERR_ACCOUNT=\u7528\u6237\u8D26\u6237\u5F02\u5E38
ERR_USEREXIST=\u8BE5\u7528\u6237\u5DF2\u7ECF\u5B58\u5728
ERR_PASSWORDSIMPLE=\u7528\u6237\u5BC6\u7801\u5F3A\u5EA6\u4E0D\u7B26\u5408\u8981\u6C42
ERR_INPUTFORMAT=\u8F93\u5165\u4FE1\u606F\u683C\u5F0F\u6709\u8BEF
ERR_INPUTMISS=\u7F3A\u5C11\u5FC5\u8981\u8F93\u5165\u4FE1\u606F
ERR_PERMISSION=\u64CD\u4F5C\u5458\u6743\u9650\u4E0D\u8DB3
ERR_REQTIMEOUT=\u8BF7\u6C42\u8D85\u65F6
ERR_PARAMS=\u53C2\u6570\u9519\u8BEF
ERR_SYSTEMEXCEPTION=\u7CFB\u7EDF\u5F02\u5E38
ERR_UNKNOWNCMD=\u672A\u77E5\u547D\u4EE4
ERR_LOGOUT=\u7528\u6237\u672A\u767B\u5F55
ERR_TOKENTIMEOUT=Token\u8D85\u65F6
ERR_TOKENNOTFOUND=\u975E\u6CD5Token
ERR_TOKEN_KEY=Token \u79D8\u94A5\u9519\u8BEF
ERR_MISSAUTHHEAD=Http \u8BF7\u6C42\u7F3A\u5C11\u8BA4\u8BC1\u5934\u90E8
ERR_NOSUCHITEM=\u6CA1\u6709\u8BE5\u5185\u5BB9
ERR_ITEMEXISTS=\u8BE5\u5185\u5BB9\u5DF2\u7ECF\u5B58\u5728
ERR_PARAMEXCEPTION=\u53C2\u6570\u5F02\u5E38
ERR_DEVICELOCKED=\u8BBE\u5907\u5DF2\u9501\u5B9A
ERR_VERSION=\u534F\u8BAE\u7248\u672C\u4E0D\u517C\u5BB9\uFF0C\u8BF7\u5347\u7EA7\u7CFB\u7EDF
ERR_NOSUCHTYPE=\u6CA1\u6709\u8FD9\u4E2A\u7C7B\u578B\u7684\u8BBE\u5907
ERR_REMOVEMORE=\u7981\u6B62\u540C\u65F6\u5220\u9664\u591A\u4E2A\u8BBE\u5907
ERR_TASKRUNNING=\u540C\u7C7B\u4EFB\u52A1\u6B63\u5728\u8FD0\u884C
ERR_UNSUPPORT=\u4E0D\u652F\u6301\u7684\u64CD\u4F5C
ERR_INTERRUPT=\u64CD\u4F5C\u4E2D\u65AD
ERR_CALLDEVICE=\u8C03\u7528\u8BBE\u5907\u5931\u8D25
ERR_NOSUCHTASK=\u6CA1\u6709\u8BE5\u4EFB\u52A1
ERR_TASKNOTRUNNING=\u8BE5\u4EFB\u52A1\u6CA1\u6709\u8FD0\u884C
ERR_REQUESTTIMEOUT=\u8BF7\u6C42\u8D85\u65F6
ERR_UNABLEDISPOSEIP=\u65E0\u6CD5\u5904\u7F6E\u8BE5IP
ERR_DATABASE=\u64CD\u4F5C\u6570\u636E\u5E93\u5931\u8D25
ERR_UNTRUSTHOST=\u672A\u7ECF\u6388\u6743\u7684\u5BA2\u6237\u7AEF
ERR_UNTRUSTTOKEN=\u672A\u7ECF\u6388\u6743\u7684Token
ERR_UNKNOWNINTERFACE=\u672A\u63D0\u4F9B\u8BE5\u63A5\u53E3
ERR_DECRYPT_BASE64=BASE64\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_BASE64=BASE64\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_AES128=AES128\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_AES128=AES128\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_3DES=3DES\u89E3\u5BC6\u5931\u8D25
ERR_ENCRYPT_3DES=3DES\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_UNKNOWN=\u4E0D\u652F\u6301\u7684\u89E3\u5BC6\u7B97\u6CD5
ERR_ENCRYPT_UNKNOWN=\u4E0D\u652F\u6301\u7684\u52A0\u5BC6\u7B97\u6CD5
ERR_JSON_ENCODE=Json \u5E8F\u5217\u53F7\u9519\u8BEF
ERR_JSON_DECODE=Json \u53CD\u5E8F\u5217\u5316\u9519\u8BEF
ERR_ENCRYPT_AES256=AES256\u52A0\u5BC6\u5931\u8D25
ERR_DECRYPT_AES256=AES256\u89E3\u5BC6\u5931\u8D25
ERR_CRYPTO_KEY=\u9519\u8BEF\u7684\u79D8\u94A5
ERR_USER_ROLE_NOTEXISTS=\u7528\u6237\u89D2\u8272\u4E0D\u5B58\u5728
ERR_RESOURCE_USED=\u8D44\u6E90\u88AB\u5360\u7528