REM:
1. 增加请求协议加密异常处理功能
This commit is contained in:
HuangXin 2020-08-25 18:36:04 +08:00
parent 2b0b8ae0a9
commit f7cd5d0d3a
4 changed files with 82 additions and 20 deletions

View File

@ -157,9 +157,42 @@ public enum ErrorCode {
ERR_HAOHAN_ERROR(34, "浩瀚设备返回错误"),
/**
* Err database error code.
* The Err database.
*/
ERR_DATABASE(35, "操作数据库失败"),
/**
* The Err decrypt base 64.
*/
ERR_DECRYPT_BASE64(100, "BASE64解密失败"),
/**
* The Err encrypt base 64.
*/
ERR_ENCRYPT_BASE64(101, "BASE64加密失败"),
/**
* The Err decrypt aes 256.
*/
ERR_DECRYPT_AES256(102, "AES256解密失败"),
/**
* The Err encrypt aes 256.
*/
ERR_ENCRYPT_AES256(103, "AES256加密失败"),
/**
* The Err decrypt 3 des.
*/
ERR_DECRYPT_3DES(104, "3DES解密失败"),
/**
* The Err encrypt 3 des.
*/
ERR_ENCRYPT_3DES(105, "3DES加密失败"),
/**
* Err decrypt unknown error code.
*/
ERR_DECRYPT_UNKNOWN(106, "不支持的解密算法"),
/**
* Err encrypt unknown error code.
*/
ERR_ENCRYPT_UNKNOWN(107, "不支持的加密算法"),
;
/**

View File

@ -3,6 +3,7 @@ package com.dispose.exception;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.dto.protocol.base.BaseRespStatus;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.security.exception.SecurityProtocolException;
import jodd.net.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
@ -51,7 +52,12 @@ public class GlobalExceptionHandler {
public ProtocolRespDTO<BaseRespStatus> handleException(Throwable ex) {
log.error("Throwable Exception: ", ex);
if (ex instanceof SecurityProtocolException) {
return ProtocolRespDTO.result(((SecurityProtocolException) ex).getErr(),
HttpStatus.error400().status(), new String[]{((SecurityProtocolException) ex).getErr().getMsg()});
} else {
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION,
HttpStatus.error400().status(), new String[]{ErrorCode.ERR_PARAMEXCEPTION.getMsg()});
}
}
}

View File

@ -0,0 +1,29 @@
package com.security.exception;
import com.dispose.common.ErrorCode;
import lombok.Getter;
import lombok.Setter;
/**
* The type Security exception.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@Getter
@Setter
public class SecurityProtocolException extends RuntimeException {
/**
* The Err.
*/
private ErrorCode err;
/**
* Instantiates a new Security exception.
*
* @param err the err
*/
public SecurityProtocolException(ErrorCode err) {
super();
this.err = err;
}
}

View File

@ -1,5 +1,6 @@
package com.security.protocol;
import com.dispose.common.ErrorCode;
import com.dispose.common.ProtoCryptoType;
import com.dispose.common.SecurityConfigValue;
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
@ -7,6 +8,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.security.arithmetic.CryptoHelper;
import com.security.exception.SecurityProtocolException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.Contract;
@ -14,15 +16,9 @@ import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
/**
* The type Decrypt request protocol.
@ -90,22 +86,20 @@ public class DecryptRequestProtocol implements HttpInputMessage {
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) {
try {
decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
| IllegalBlockSizeException e) {
} catch (Exception e) {
log.error("AES256 decode message error: {}", base64Decode);
decryptContent = base64Decode;
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256);
}
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) {
try {
decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY);
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException
| BadPaddingException | IllegalBlockSizeException e) {
} catch (Exception e) {
log.error("DES256 decode message error: {}", base64Decode);
decryptContent = base64Decode;
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_3DES);
}
} else {
log.error("Unknown protocol security type: {}, {}", proReq.getCryptoType(), inputMessage.getBody());
return inputMessage.getBody();
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_UNKNOWN);
}
// 字节数组转换为字符串