REM:
1. 协议支持AES256加密算法
This commit is contained in:
HuangXin 2020-09-16 17:36:04 +08:00
parent cb28566904
commit ca831439bb
3 changed files with 78 additions and 50 deletions

View File

@ -9,202 +9,210 @@ import javax.servlet.http.HttpServletResponse;
*/ */
public enum ErrorCode { public enum ErrorCode {
/** /**
* Err ok error code. * The Err ok.
*/ */
ERR_OK(0, "成功"), ERR_OK(0, "成功"),
/** /**
* Err password error code. * The Err password.
*/ */
ERR_PASSWORD(1, "密码错误"), ERR_PASSWORD(1, "密码错误"),
/** /**
* Err usernotfound error code. * The Err usernotfound.
*/ */
ERR_USERNOTFOUND(2, "用户不存在"), ERR_USERNOTFOUND(2, "用户不存在"),
/** /**
* Err passwordmore error code. * The Err passwordmore.
*/ */
ERR_PASSWORDMORE(3, "连续密码错误达上限,再次输入错误将锁定用户"), ERR_PASSWORDMORE(3, "连续密码错误达上限,再次输入错误将锁定用户"),
/** /**
* Err userlock error code. * The Err userlock.
*/ */
ERR_USERLOCK(4, "密码错误达上限,用户被锁定"), ERR_USERLOCK(4, "密码错误达上限,用户被锁定"),
/** /**
* Err account error code. * The Err account.
*/ */
ERR_ACCOUNT(5, "用户账户异常"), ERR_ACCOUNT(5, "用户账户异常"),
/** /**
* Err userexist error code. * The Err userexist.
*/ */
ERR_USEREXIST(6, "该用户已经存在"), ERR_USEREXIST(6, "该用户已经存在"),
/** /**
* Err passwordsimple error code. * The Err passwordsimple.
*/ */
ERR_PASSWORDSIMPLE(7, "用户密码强度不符合要求"), ERR_PASSWORDSIMPLE(7, "用户密码强度不符合要求"),
/** /**
* Err inputformat error code. * The Err inputformat.
*/ */
ERR_INPUTFORMAT(8, "输入信息格式有误"), ERR_INPUTFORMAT(8, "输入信息格式有误"),
/** /**
* Err inputmiss error code. * The Err inputmiss.
*/ */
ERR_INPUTMISS(9, "缺少必要输入信息"), ERR_INPUTMISS(9, "缺少必要输入信息"),
/** /**
* Err permission error code. * The Err permission.
*/ */
ERR_PERMISSION(10, "操作员权限不足"), ERR_PERMISSION(10, "操作员权限不足"),
/** /**
* Err reqtimeout error code. * The Err reqtimeout.
*/ */
ERR_REQTIMEOUT(11, "请求超时"), ERR_REQTIMEOUT(11, "请求超时"),
/** /**
* Err params error code. * The Err params.
*/ */
ERR_PARAMS(12, "参数错误"), ERR_PARAMS(12, "参数错误"),
/** /**
* Err systemexception error code. * The Err systemexception.
*/ */
ERR_SYSTEMEXCEPTION(13, "系统异常"), ERR_SYSTEMEXCEPTION(13, "系统异常"),
/** /**
* Err unknowncmd error code. * The Err unknowncmd.
*/ */
ERR_UNKNOWNCMD(14, "未知命令"), ERR_UNKNOWNCMD(14, "未知命令"),
/** /**
* Err logout error code. * The Err logout.
*/ */
ERR_LOGOUT(15, "用户未登录"), ERR_LOGOUT(15, "用户未登录"),
/** /**
* Err tokentimeout error code. * The Err tokentimeout.
*/ */
ERR_TOKENTIMEOUT(16, "Token超时"), ERR_TOKENTIMEOUT(16, "Token超时"),
/** /**
* Err tokennotfound error code. * The Err tokennotfound.
*/ */
ERR_TOKENNOTFOUND(17, "非法Token"), ERR_TOKENNOTFOUND(17, "非法Token"),
/** /**
* Err missauthhead error code. * The Err missauthhead.
*/ */
ERR_MISSAUTHHEAD(18, "Http 请求缺少认证头部"), ERR_MISSAUTHHEAD(18, "Http 请求缺少认证头部"),
/** /**
* Err nosuchdevice error code. * The Err nosuchdevice.
*/ */
ERR_NOSUCHDEVICE(19, "没有这个设备"), ERR_NOSUCHDEVICE(19, "没有这个设备"),
/** /**
* Err deviceexists error code. * The Err deviceexists.
*/ */
ERR_DEVICEEXISTS(20, "设备已经存在"), ERR_DEVICEEXISTS(20, "设备已经存在"),
/** /**
* Err paramexception error code. * The Err paramexception.
*/ */
ERR_PARAMEXCEPTION(21, "参数异常"), ERR_PARAMEXCEPTION(21, "参数异常"),
/** /**
* Err devicelocked error code. * The Err devicelocked.
*/ */
ERR_DEVICELOCKED(22, "设备已锁定"), ERR_DEVICELOCKED(22, "设备已锁定"),
/** /**
* Err version error code. * The Err version.
*/ */
ERR_VERSION(23, "协议版本不兼容,请升级系统"), ERR_VERSION(23, "协议版本不兼容,请升级系统"),
/** /**
* Err nosuchtype error code. * The Err nosuchtype.
*/ */
ERR_NOSUCHTYPE(24, "没有这个类型的处置设备"), ERR_NOSUCHTYPE(24, "没有这个类型的处置设备"),
/** /**
* Err removemore error code. * The Err removemore.
*/ */
ERR_REMOVEMORE(25, "禁止同时删除多个设备"), ERR_REMOVEMORE(25, "禁止同时删除多个设备"),
/** /**
* Err taskrunning error code. * The Err taskrunning.
*/ */
ERR_TASKRUNNING(26, "同类任务正在运行"), ERR_TASKRUNNING(26, "同类任务正在运行"),
/** /**
* Err unsupport error code. * The Err unsupport.
*/ */
ERR_UNSUPPORT(27, "不支持的操作"), ERR_UNSUPPORT(27, "不支持的操作"),
/** /**
* Err interrupt error code. * The Err interrupt.
*/ */
ERR_INTERRUPT(28, "操作中断"), ERR_INTERRUPT(28, "操作中断"),
/** /**
* Err calldevice error code. * The Err calldevice.
*/ */
ERR_CALLDEVICE(29, "调用设备失败"), ERR_CALLDEVICE(29, "调用设备失败"),
/** /**
* Err nosuchtask error code. * The Err nosuchtask.
*/ */
ERR_NOSUCHTASK(30, "没有该任务"), ERR_NOSUCHTASK(30, "没有该任务"),
/** /**
* Err tasknotrunning error code. * The Err tasknotrunning.
*/ */
ERR_TASKNOTRUNNING(31, "该任务没有运行"), ERR_TASKNOTRUNNING(31, "该任务没有运行"),
/** /**
* Err requesttimeout error code. * The Err requesttimeout.
*/ */
ERR_REQUESTTIMEOUT(32, "请求超时"), ERR_REQUESTTIMEOUT(32, "请求超时"),
/** /**
* Err unabledisposeip error code. * The Err unabledisposeip.
*/ */
ERR_UNABLEDISPOSEIP(33, "无法处置该IP"), ERR_UNABLEDISPOSEIP(33, "无法处置该IP"),
/** /**
* Err haohan error error code. * The Err haohan error.
*/ */
ERR_HAOHAN_ERROR(34, "浩瀚设备返回错误"), ERR_HAOHAN_ERROR(34, "浩瀚设备返回错误"),
/** /**
* Err database error code. * The Err database.
*/ */
ERR_DATABASE(35, "操作数据库失败"), ERR_DATABASE(35, "操作数据库失败"),
/** /**
* Err ipnodevice error code. * The Err ipnodevice.
*/ */
ERR_IPNODEVICE(36, "找不到处置该IP的设备"), ERR_IPNODEVICE(36, "找不到处置该IP的设备"),
/** /**
* Err untrusthost error code. * The Err untrusthost.
*/ */
ERR_UNTRUSTHOST(37, "未经授权的客户端"), ERR_UNTRUSTHOST(37, "未经授权的客户端"),
/** /**
* Err untrusttoken error code. * The Err untrusttoken.
*/ */
ERR_UNTRUSTTOKEN(38, "未经授权的Token"), ERR_UNTRUSTTOKEN(38, "未经授权的Token"),
/** /**
* Err decrypt base 64 error code. * The Err decrypt base 64.
*/ */
ERR_DECRYPT_BASE64(100, "BASE64解密失败"), ERR_DECRYPT_BASE64(100, "BASE64解密失败"),
/** /**
* Err encrypt base 64 error code. * The Err encrypt base 64.
*/ */
ERR_ENCRYPT_BASE64(101, "BASE64加密失败"), ERR_ENCRYPT_BASE64(101, "BASE64加密失败"),
/** /**
* Err decrypt aes 256 error code. * The Err decrypt aes 128.
*/ */
ERR_DECRYPT_AES128(102, "AES128解密失败"), ERR_DECRYPT_AES128(102, "AES128解密失败"),
/** /**
* Err encrypt aes 256 error code. * The Err encrypt aes 128.
*/ */
ERR_ENCRYPT_AES128(103, "AES128加密失败"), ERR_ENCRYPT_AES128(103, "AES128加密失败"),
/** /**
* Err decrypt 3 des error code. * The Err decrypt 3 des.
*/ */
ERR_DECRYPT_3DES(104, "3DES解密失败"), ERR_DECRYPT_3DES(104, "3DES解密失败"),
/** /**
* Err encrypt 3 des error code. * The Err encrypt 3 des.
*/ */
ERR_ENCRYPT_3DES(105, "3DES加密失败"), ERR_ENCRYPT_3DES(105, "3DES加密失败"),
/** /**
* Err decrypt unknown error code. * The Err decrypt unknown.
*/ */
ERR_DECRYPT_UNKNOWN(106, "不支持的解密算法"), ERR_DECRYPT_UNKNOWN(106, "不支持的解密算法"),
/** /**
* Err encrypt unknown error code. * The Err encrypt unknown.
*/ */
ERR_ENCRYPT_UNKNOWN(107, "不支持的加密算法"), ERR_ENCRYPT_UNKNOWN(107, "不支持的加密算法"),
/** /**
* Err json encode error code. * The Err json encode.
*/ */
ERR_JSON_ENCODE(108, "Json 序列号错误"), ERR_JSON_ENCODE(108, "Json 序列号错误"),
/** /**
* Err json decode error code. * The Err json decode.
*/ */
ERR_JSON_DECODE(109, "Json 反序列化错误"), ERR_JSON_DECODE(109, "Json 反序列化错误"),
/**
* The Err encrypt aes 256.
*/
ERR_ENCRYPT_AES256(110, "AES256加密失败"),
/**
* The Err decrypt aes 256.
*/
ERR_DECRYPT_AES256(111, "AES256解密失败"),
; ;
/** /**

View File

@ -15,13 +15,17 @@ public enum ProtoCryptoType {
*/ */
CRYPTO_BASE64(1, "Base64编码"), CRYPTO_BASE64(1, "Base64编码"),
/** /**
* The Crypto aes 256. * The Crypto aes 128.
*/ */
CRYPTO_AES128(2, "AES128加密"), CRYPTO_AES128(2, "AES128加密"),
/** /**
* The Crypto des. * The Crypto des.
*/ */
CRYPTO_DES(3, "DES对称加密"), CRYPTO_DES(3, "DES对称加密"),
/**
* Crypto aes 256 proto crypto type.
*/
CRYPTO_AES256(4, "AES256加密"),
; ;
/** /**

View File

@ -77,6 +77,13 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
log.error("AES128 decode message error: {}", base64Decode); log.error("AES128 decode message error: {}", base64Decode);
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES128); throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES128);
} }
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) {
try {
decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY);
} catch (Exception e) {
log.error("AES256 decode message error: {}", base64Decode);
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256);
}
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) { } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) {
try { try {
decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY); decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY);
@ -131,6 +138,15 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
log.error("AES128 encode message error: {}", plainText); log.error("AES128 encode message error: {}", plainText);
throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_AES128); throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_AES128);
} }
} else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) {
try {
byte[] encode = CryptoHelper.aes256Encryption(plainText.getBytes(StandardCharsets.UTF_8),
SecurityConfigValue.AES_KEY);
cipherText = CryptoHelper.base64Encryption(encode);
} catch (Exception e) {
log.error("AES256 encode message error: {}", plainText);
throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_AES256);
}
} else if (cryptoType == ProtoCryptoType.CRYPTO_DES.getCode()) { } else if (cryptoType == ProtoCryptoType.CRYPTO_DES.getCode()) {
try { try {
byte[] encode = CryptoHelper.desEncryption(plainText.getBytes(StandardCharsets.UTF_8), byte[] encode = CryptoHelper.desEncryption(plainText.getBytes(StandardCharsets.UTF_8),