From f7cd5d0d3a3501b43fcc6da8146f4c36b4bc8768 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 25 Aug 2020 18:36:04 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8D=8F=E8=AE=AE=E5=8A=A0=E5=AF=86=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/ErrorCode.java | 35 ++++++++++++++++++- .../exception/GlobalExceptionHandler.java | 18 ++++++---- .../exception/SecurityProtocolException.java | 29 +++++++++++++++ .../protocol/DecryptRequestProtocol.java | 20 ++++------- 4 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/security/exception/SecurityProtocolException.java diff --git a/src/main/java/com/dispose/common/ErrorCode.java b/src/main/java/com/dispose/common/ErrorCode.java index 59acf7cb..277b44a6 100644 --- a/src/main/java/com/dispose/common/ErrorCode.java +++ b/src/main/java/com/dispose/common/ErrorCode.java @@ -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, "不支持的加密算法"), ; /** diff --git a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java index d0c0db7b..6d166cd7 100644 --- a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java @@ -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; @@ -38,12 +39,12 @@ public class GlobalExceptionHandler { AtomicInteger idx = new AtomicInteger(); ex.getBindingResult() - .getAllErrors() - .forEach(v -> exMsg.add(idx.getAndIncrement() + ": " + v.getDefaultMessage())); + .getAllErrors() + .forEach(v -> exMsg.add(idx.getAndIncrement() + ": " + v.getDefaultMessage())); return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, - HttpStatus.error400().status(), - exMsg.toArray(new String[0])); + HttpStatus.error400().status(), + exMsg.toArray(new String[0])); } @ExceptionHandler(Throwable.class) @@ -51,7 +52,12 @@ public class GlobalExceptionHandler { public ProtocolRespDTO handleException(Throwable ex) { log.error("Throwable Exception: ", ex); - return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, - HttpStatus.error400().status(), new String[] {ErrorCode.ERR_PARAMEXCEPTION.getMsg()}); + 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()}); + } } } diff --git a/src/main/java/com/security/exception/SecurityProtocolException.java b/src/main/java/com/security/exception/SecurityProtocolException.java new file mode 100644 index 00000000..3dd66a05 --- /dev/null +++ b/src/main/java/com/security/exception/SecurityProtocolException.java @@ -0,0 +1,29 @@ +package com.security.exception; + +import com.dispose.common.ErrorCode; +import lombok.Getter; +import lombok.Setter; + +/** + * The type Security exception. + * + * @author + */ +@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; + } +} diff --git a/src/main/java/com/security/protocol/DecryptRequestProtocol.java b/src/main/java/com/security/protocol/DecryptRequestProtocol.java index 4a29e7ad..3fa0f63d 100644 --- a/src/main/java/com/security/protocol/DecryptRequestProtocol.java +++ b/src/main/java/com/security/protocol/DecryptRequestProtocol.java @@ -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); } // 字节数组转换为字符串