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) { } }