package com.qianwen.core.api.crypto.core;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.lang.reflect.Type;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.api.crypto.annotation.decrypt.ApiDecrypt;
|
import com.qianwen.core.api.crypto.bean.CryptoInfoBean;
|
import com.qianwen.core.api.crypto.bean.DecryptHttpInputMessage;
|
import com.qianwen.core.api.crypto.config.ApiCryptoProperties;
|
import com.qianwen.core.api.crypto.exception.DecryptBodyFailException;
|
import com.qianwen.core.api.crypto.util.ApiCryptoUtil;
|
import com.qianwen.core.tool.utils.ClassUtil;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.context.annotation.Configuration;
|
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;
|
|
@Configuration(proxyBeanMethods = false)
|
@ControllerAdvice
|
@ConditionalOnProperty(value = {"blade.api.crypto.enabled"}, havingValue = "true", matchIfMissing = true)
|
@Order(1)
|
/* loaded from: blade-starter-api-crypto-9.3.0.0-SNAPSHOT.jar:org/springblade/core/api/crypto/core/ApiDecryptRequestBodyAdvice.class */
|
public class ApiDecryptRequestBodyAdvice implements RequestBodyAdvice {
|
private static final Logger log = LoggerFactory.getLogger(ApiDecryptRequestBodyAdvice.class);
|
private final ApiCryptoProperties properties;
|
|
public ApiDecryptRequestBodyAdvice(final ApiCryptoProperties properties) {
|
this.properties = properties;
|
}
|
|
public boolean supports(MethodParameter methodParameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
|
return ClassUtil.isAnnotated(methodParameter.getMethod(), ApiDecrypt.class);
|
}
|
|
public Object handleEmptyBody(Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
|
return body;
|
}
|
|
@NonNull
|
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, @NonNull MethodParameter parameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
InputStream messageBody = inputMessage.getBody();
|
if (messageBody.available() <= 0) {
|
return inputMessage;
|
}
|
byte[] decryptedBody = null;
|
CryptoInfoBean cryptoInfoBean = ApiCryptoUtil.getDecryptInfo(parameter);
|
if (cryptoInfoBean != null) {
|
byte[] bodyByteArray = StreamUtils.copyToByteArray(messageBody);
|
decryptedBody = ApiCryptoUtil.decryptData(this.properties, bodyByteArray, cryptoInfoBean);
|
}
|
if (decryptedBody == null) {
|
throw new DecryptBodyFailException("Decryption error, please check if the selected source data is encrypted correctly. (解密错误,请检查选择的源数据的加密方式是否正确。)");
|
}
|
InputStream inputStream = new ByteArrayInputStream(decryptedBody);
|
return new DecryptHttpInputMessage(inputStream, inputMessage.getHeaders());
|
}
|
|
@NonNull
|
public Object afterBodyRead(@NonNull Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
|
return body;
|
}
|
}
|