package com.qianwen.core.api.crypto.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.core.api.crypto.annotation.encrypt.ApiEncrypt; import com.qianwen.core.api.crypto.bean.CryptoInfoBean; import com.qianwen.core.api.crypto.config.ApiCryptoProperties; import com.qianwen.core.api.crypto.exception.EncryptBodyFailException; import com.qianwen.core.api.crypto.util.ApiCryptoUtil; import com.qianwen.core.tool.jackson.JsonUtil; 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.MediaType; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; @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/ApiEncryptResponseBodyAdvice.class */ public class ApiEncryptResponseBodyAdvice implements ResponseBodyAdvice { private static final Logger log = LoggerFactory.getLogger(ApiEncryptResponseBodyAdvice.class); private final ApiCryptoProperties properties; public ApiEncryptResponseBodyAdvice(final ApiCryptoProperties properties) { this.properties = properties; } public boolean supports(MethodParameter returnType, @NonNull Class converterType) { return ClassUtil.isAnnotated(returnType.getMethod(), ApiEncrypt.class); } @Nullable public Object beforeBodyWrite(Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType, @NonNull Class selectedConverterType, @NonNull ServerHttpRequest request, @NonNull ServerHttpResponse response) { if (body == null) { return null; } response.getHeaders().setContentType(MediaType.TEXT_PLAIN); CryptoInfoBean cryptoInfoBean = ApiCryptoUtil.getEncryptInfo(returnType); if (cryptoInfoBean != null) { byte[] bodyJsonBytes = JsonUtil.toJsonAsBytes(body); return ApiCryptoUtil.encryptData(this.properties, bodyJsonBytes, cryptoInfoBean); } throw new EncryptBodyFailException(); } }