From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期五, 01 十一月 2024 15:11:19 +0800
Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成"

---
 Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/core/ApiDecryptRequestBodyAdvice.java |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/core/ApiDecryptRequestBodyAdvice.java b/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/core/ApiDecryptRequestBodyAdvice.java
new file mode 100644
index 0000000..28075c0
--- /dev/null
+++ b/Source/BladeX-Tool/blade-starter-api-crypto/src/main/java/org/springblade/core/api/crypto/core/ApiDecryptRequestBodyAdvice.java
@@ -0,0 +1,87 @@
+package org.springblade.core.api.crypto.core;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springblade.core.api.crypto.annotation.decrypt.ApiDecrypt;
+import org.springblade.core.api.crypto.bean.CryptoInfoBean;
+import org.springblade.core.api.crypto.bean.DecryptHttpInputMessage;
+import org.springblade.core.api.crypto.config.ApiCryptoProperties;
+import org.springblade.core.api.crypto.exception.DecryptBodyFailException;
+import org.springblade.core.api.crypto.util.ApiCryptoUtil;
+import org.springblade.core.tool.utils.ClassUtil;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.core.MethodParameter;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.HttpInputMessage;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.lang.NonNull;
+import org.springframework.util.StreamUtils;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Type;
+
+/**
+ * 璇锋眰鏁版嵁鐨勫姞瀵嗕俊鎭В瀵嗗鐞�<br>
+ * 鏈被鍙鎺у埗鍣ㄥ弬鏁颁腑鍚湁<strong>{@link org.springframework.web.bind.annotation.RequestBody}</strong>
+ * 浠ュ強package涓�<strong><code>org.springblade.core.api.signature.annotation.decrypt</code></strong>涓嬬殑娉ㄨВ鏈夋晥
+ *
+ * @author licoy.cn, L.cm
+ * @see RequestBodyAdvice
+ */
+@Slf4j
+@Order(1)
+@AutoConfiguration
+@ControllerAdvice
+@RequiredArgsConstructor
+@ConditionalOnProperty(value = ApiCryptoProperties.PREFIX + ".enabled", havingValue = "true", matchIfMissing = true)
+public class ApiDecryptRequestBodyAdvice implements RequestBodyAdvice {
+	private final ApiCryptoProperties properties;
+
+	@Override
+	public boolean supports(MethodParameter methodParameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
+		return ClassUtil.isAnnotated(methodParameter.getMethod(), ApiDecrypt.class);
+	}
+
+	@Override
+	public Object handleEmptyBody(Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter,
+								  @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
+		return body;
+	}
+
+	@NonNull
+	@Override
+	public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, @NonNull MethodParameter parameter,
+										   @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
+		// 鍒ゆ柇 body 鏄惁涓虹┖
+		InputStream messageBody = inputMessage.getBody();
+		if (messageBody.available() <= 0) {
+			return inputMessage;
+		}
+		byte[] decryptedBody = null;
+		CryptoInfoBean cryptoInfoBean = ApiCryptoUtil.getDecryptInfo(parameter);
+		if (cryptoInfoBean != null) {
+			// base64 byte array
+			byte[] bodyByteArray = StreamUtils.copyToByteArray(messageBody);
+			decryptedBody = ApiCryptoUtil.decryptData(properties, bodyByteArray, cryptoInfoBean);
+		}
+		if (decryptedBody == null) {
+			throw new DecryptBodyFailException("Decryption error, " +
+				"please check if the selected source data is encrypted correctly." +
+				" (瑙e瘑閿欒锛岃妫�鏌ラ�夋嫨鐨勬簮鏁版嵁鐨勫姞瀵嗘柟寮忔槸鍚︽纭��)");
+		}
+		InputStream inputStream = new ByteArrayInputStream(decryptedBody);
+		return new DecryptHttpInputMessage(inputStream, inputMessage.getHeaders());
+	}
+
+	@NonNull
+	@Override
+	public Object afterBodyRead(@NonNull Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
+		return body;
+	}
+
+}

--
Gitblit v1.9.3