PC
2024-03-30 48c8f6440a6c0b0c060467a0633bc669ca255345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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<Object> {
    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();
    }
}