MyBatis 플러그인 수정
parent
e6357b5cd6
commit
5ed401050c
@ -0,0 +1,133 @@
|
||||
package cokr.xit.foundation.data.paging;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.executor.resultset.ResultSetHandler;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.plugin.Intercepts;
|
||||
import org.apache.ibatis.plugin.Signature;
|
||||
|
||||
import cokr.xit.foundation.data.ARIA;
|
||||
|
||||
@Intercepts({
|
||||
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}),
|
||||
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
|
||||
})
|
||||
public class EncryptionSupport extends MybatisPlugin {
|
||||
private boolean enabled;
|
||||
private ARIA aria = new ARIA();
|
||||
|
||||
private List<String> decrypted = Collections.emptyList();
|
||||
private List<Adaptor> adaptors = Collections.emptyList();
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
String str = properties.getProperty("enabled", "");
|
||||
enabled = !"false".equals(str);
|
||||
|
||||
str = properties.getProperty("enc.key");
|
||||
aria.setKey(str);
|
||||
str = properties.getProperty("enc.algorithm");
|
||||
if (!isEmpty(str))
|
||||
aria.setAlgorithm(str);
|
||||
|
||||
str = properties.getProperty("dec.fields", "");
|
||||
if (!isEmpty(str))
|
||||
decrypted = List.of(str.split(","));
|
||||
|
||||
str = properties.getProperty("enc.adaptor", "");
|
||||
if (isEmpty(str)) return;
|
||||
|
||||
adaptors = Stream.of(str.split(",")).map(name -> {
|
||||
try {
|
||||
Class<?> klass = Class.forName(name);
|
||||
Adaptor adaptor = (Adaptor)klass.getConstructor().newInstance();
|
||||
adaptor.setARIA(aria);
|
||||
return adaptor;
|
||||
} catch (Exception e) {
|
||||
throw runtimeException(e);
|
||||
}
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object handle(ResultSetHandler resultSetHandler, Statement statement) throws SQLException {
|
||||
Object obj = super.handle(resultSetHandler, statement);
|
||||
|
||||
if (obj instanceof List) {
|
||||
List<?> list = (List<?>)obj;
|
||||
list.forEach(this::decrypt);
|
||||
} else {
|
||||
decrypt(obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private void decrypt(Object obj) {
|
||||
if (!enabled || decrypted.isEmpty()) return;
|
||||
|
||||
if (obj instanceof Map)
|
||||
decrypt((Map<String, Object>)obj);
|
||||
}
|
||||
|
||||
protected void decrypt(Map<String, Object> map) {
|
||||
decrypted.forEach(k -> {
|
||||
if (!map.containsKey(k)) return;
|
||||
|
||||
Object v = map.get(k);
|
||||
map.put(k, aria.decrypt((String)v));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(Executor executor, MappedStatement mappedStatement, Object obj) throws SQLException {
|
||||
switch (mappedStatement.getSqlCommandType()) {
|
||||
case INSERT:
|
||||
case UPDATE: encrypt(obj); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return super.update(executor, mappedStatement, obj);
|
||||
}
|
||||
|
||||
private void encrypt(Object obj) {
|
||||
if (!enabled || obj == null || adaptors.isEmpty()) return;
|
||||
|
||||
process(obj, arg -> adaptors.forEach(adaptor -> encrypt(adaptor, arg)));
|
||||
}
|
||||
|
||||
private void encrypt(Adaptor adaptor, Object obj) {
|
||||
if (!adaptor.setTarget(obj)) return;
|
||||
|
||||
adaptor.encrypt();
|
||||
adaptor.clear();
|
||||
}
|
||||
|
||||
public abstract static class Adaptor {
|
||||
protected ARIA aria;
|
||||
protected Object obj;
|
||||
|
||||
public Adaptor setARIA(ARIA aria) {
|
||||
this.aria = aria;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean setTarget(Object obj) {
|
||||
this.obj = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract void encrypt();
|
||||
|
||||
public void clear() {
|
||||
obj = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue