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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package app.pooi.common.encrypt;
import app.pooi.common.encrypt.anno.CipherSpi; import app.pooi.common.encrypt.anno.Encrypted; import lombok.Getter; import org.apache.ibatis.executor.resultset.ResultSetHandler; import org.apache.ibatis.plugin.*; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject;
import java.lang.reflect.Field; import java.sql.Statement; import java.util.*; import java.util.function.Function; import java.util.logging.Logger; import java.util.stream.Collectors;
@Intercepts({ @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}), }) public class DecryptInterceptor implements Interceptor {
private static final Logger logger = Logger.getLogger(DecryptInterceptor.class.getName());
private CipherSpi cipherSpi;
public DecryptInterceptor(CipherSpi cipherSpi) { this.cipherSpi = cipherSpi; }
@Override public Object intercept(Invocation invocation) throws Throwable {
final Object proceed = invocation.proceed();
if (proceed == null) { return proceed; }
List<?> results = (List<?>) proceed;
if (results.isEmpty()) { return proceed; }
final Object first = results.iterator().next();
final Class<?> modelClazz = first.getClass();
final List<String> decryptFields = getDecryptFields(modelClazz);
if (decryptFields.isEmpty()) { return proceed; }
final List<List<String>> secret = Flux.fromIterable(results) .map(SystemMetaObject::forObject) .flatMapIterable(mo -> decryptFields.stream().map(mo::getValue).collect(Collectors.toList())) .cast(String.class) .buffer(1000) .collectList() .block();
final Map<String, String> secretMap = secret.stream() .map(secrets -> { try { return cipherSpi.batchDecrypt(secrets); } catch (Exception e) { e.printStackTrace(); return Maps.<String, String>newHashMap(); } }).reduce(Maps.newHashMap(), (m1, m2) -> { m1.putAll(m2); return m1; });
secretMap.put("", "0");
for (Object r : results) { final MetaObject metaObject = SystemMetaObject.forObject(r); decryptFields.forEach(f -> metaObject.setValue(f, secretMap.get(metaObject.getValue(f)))); }
return results; }
@NotNull private List<String> getDecryptFields(Class<?> modelClazz) { return Arrays.stream(modelClazz.getDeclaredFields()) .filter(f -> f.getAnnotation(Decrypted.class) != null) .filter(f -> { boolean isString = f.getType() == String.class; if (!isString) { logger.warning(f.getName() + "is not String, actual type is " + f.getType().getSimpleName() + " ignored"); } return isString; }) .map(Field::getName) .collect(Collectors.toList()); }
@Override public Object plugin(Object target) { return Plugin.wrap(target, this); }
@Override public void setProperties(Properties properties) {
} }
@Getter class Tuple2<T1, T2> {
private final T1 t1;
private final T2 t2;
Tuple2(T1 t1, T2 t2) { this.t1 = t1; this.t2 = t2; }
static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); } }
|