huanghongfa
2021-09-02 177249c76aeea0b4bf8d8816d4994e3b445b45ce
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/encrypt/ParammeterInterceptor.java
@@ -1,11 +1,12 @@
package com.panzhihua.service_community.model.helper.encrypt;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import com.panzhihua.common.model.helper.encrypt.EncryptDecryptClass;
import com.panzhihua.common.model.helper.encrypt.EncryptQuery;
import com.panzhihua.common.model.helper.encrypt.EncryptQueryClass;
import com.panzhihua.common.model.helper.encrypt.IEncryptDecrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.BindingException;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.parameter.ParameterHandler;
@@ -15,19 +16,20 @@
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.util.*;
import com.panzhihua.common.model.helper.encrypt.EncryptDecryptClass;
import com.panzhihua.common.model.helper.encrypt.EncryptQuery;
import com.panzhihua.common.model.helper.encrypt.EncryptQueryClass;
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 = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class),
})
@Intercepts({@Signature(type = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class),})
@ConditionalOnProperty(value = "domain.encrypt", havingValue = "true")
@Component
@Slf4j
@@ -44,13 +46,13 @@
            PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
            // 反射获取 参数对像
            Field parameterField =
                    parameterHandler.getClass().getDeclaredField("parameterObject");
            Field parameterField = parameterHandler.getClass().getDeclaredField("parameterObject");
            parameterField.setAccessible(true);
            Object parameterObject = parameterField.get(parameterHandler);
            if (Objects.nonNull(parameterObject)) {
                Class<?> parameterObjectClass = parameterObject.getClass();
                EncryptDecryptClass encryptDecryptClass = AnnotationUtils.findAnnotation(parameterObjectClass, EncryptDecryptClass.class);
                EncryptDecryptClass encryptDecryptClass =
                    AnnotationUtils.findAnnotation(parameterObjectClass, EncryptDecryptClass.class);
                if (Objects.nonNull(encryptDecryptClass)) {
                    Field[] declaredFields = parameterObjectClass.getDeclaredFields();
                    final Object encrypt = encryptDecrypt.encrypt(declaredFields, parameterObject);
@@ -76,13 +78,15 @@
                            if (entry.getKey().startsWith("param")) {
                                Object v = entry.getValue();
                                if(v!=null) {
                                    EncryptQueryClass encryptQueryClass = AnnotationUtils.findAnnotation(v.getClass(), EncryptQueryClass.class);
                                    EncryptQueryClass encryptQueryClass =
                                        AnnotationUtils.findAnnotation(v.getClass(), EncryptQueryClass.class);
                                    if (Objects.nonNull(encryptQueryClass)) {
                                        Field[] declaredFields = v.getClass().getDeclaredFields();
                                        for (Field field : declaredFields) {
                                            if (field.isAnnotationPresent(EncryptQuery.class)) {
                                                try {
                                                    final Object encrypt = encryptDecrypt.encrypt(new Field[]{field}, v);
                                                    final Object encrypt =
                                                        encryptDecrypt.encrypt(new Field[] {field}, v);
                                                    paramMap.put("encrypted", true);
                                                } catch (IllegalAccessException e) {
                                                    e.printStackTrace();