mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.panzhihua.service_equipment.model.helper.encrypt;
 
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Properties;
 
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import com.panzhihua.common.model.helper.encrypt.EncryptDecryptClass;
import com.panzhihua.common.model.helper.encrypt.IEncryptDecrypt;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * 解密拦截器
 *
 * @author cedoo
 * @since 2021-4-1 10:40:52
 */
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
@ConditionalOnProperty(value = "domain.decrypt", havingValue = "true")
@Component
@Slf4j
@Order(110)
public class ResultInterceptor implements Interceptor {
 
    @Autowired
    private IEncryptDecrypt encryptDecrypt;
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        if (Objects.isNull(result)) {
            return null;
        }
 
        if (result instanceof ArrayList) {
            ArrayList resultList = (ArrayList)result;
            if (resultList != null && resultList.size() > 0 && needToDecrypt(resultList.get(0))) {
                for (int i = 0; i < resultList.size(); i++) {
                    encryptDecrypt.decrypt(resultList.get(i));
                }
            }
        } else {
            if (needToDecrypt(result)) {
                encryptDecrypt.decrypt(result);
            }
        }
        return result;
    }
 
    public boolean needToDecrypt(Object object) {
        if (object != null) {
            Class<?> objectClass = object.getClass();
            EncryptDecryptClass encryptDecryptClass =
                AnnotationUtils.findAnnotation(objectClass, EncryptDecryptClass.class);
            return Objects.nonNull(encryptDecryptClass);
        }
        return false;
    }
 
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
 
    @Override
    public void setProperties(Properties properties) {
 
    }
}