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
79
80
81
package com.panzhihua.common.model.helper.encrypt;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import com.panzhihua.common.model.helper.AESUtil;
 
import lombok.extern.slf4j.Slf4j;
 
/***
 * 默认实现: AES 128 加密、解密 可自定义实现{@link IEncryptDecrypt }接口
 *
 * @author cedoo
 * @since 2021-4-1 10:40:52
 */
@Slf4j
@Component
public class DoEncrytDecrypt implements IEncryptDecrypt {
 
    @Value("${domain.aesKey:}")
    private String aesKey;
 
    @Override
    public <T> T encrypt(Field[] declaredFields, T parameterObject) throws IllegalAccessException {
        for (Field field : declaredFields) {
            if (field.getType() == String.class) {
                field.setAccessible(true);
                for (Annotation annotation : field.getAnnotations()) {
                    if (annotation.annotationType() == EncryptDecryptField.class
                        || annotation.annotationType() == EncryptQuery.class) {
                        log.debug("加密字段:" + field.getName());
                        Object fieldVal = field.get(parameterObject);
                        if (fieldVal != null) {
                            try {
                                String encryptedStr = AESUtil.encrypt128(fieldVal.toString(), aesKey);
                                field.set(parameterObject, encryptedStr);
                            } catch (Exception e) {
                                log.debug("加密失败");
                            }
                        }
                    }
                }
            }
        }
        return parameterObject;
    }
 
    @Override
    public <T> T decrypt(T result) throws IllegalAccessException {
 
        for (Field field : result.getClass().getDeclaredFields()) {
            try {
                field.setAccessible(true);
                if (field.get(result) != null) {
                    for (Annotation annotation : field.getAnnotations()) {
                        if (annotation.annotationType() == EncryptDecryptField.class) {
                            log.debug("解密密字段:" + field.getName());
                            String fieldVal = field.get(result).toString();
                            try {
                                String decryptVal = AESUtil.decrypt128(fieldVal, aesKey);
                                field.set(result, decryptVal != null ? decryptVal : fieldVal);
                            } catch (Exception e) {
                                /**
                                 * 兼容原始未加密数据
                                 */
                                field.set(result, fieldVal);
                            }
                        }
                    }
                }
            } catch (IllegalAccessException e) {
                throw e;
            }
        }
        return result;
    }
 
}