encrypt(...) 수정

master
mjkhan21 10 months ago
parent c3ab91c47c
commit b68ebc43f8

@ -6,26 +6,16 @@ import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import cokr.xit.base.crypto.Cryptography.Config;
import cokr.xit.base.crypto.web.CleanupCryptography;
import cokr.xit.foundation.util.ConditionSupport;
@EnableWebMvc
@Configuration
@Conditional(ConditionSupport.InBoot.class)
public class CryptoConfig implements WebMvcConfigurer {
public class CryptoConfig {
@Resource(name = "sqlSession")
private DefaultSqlSessionFactory sqlSessionFactory;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CleanupCryptography()).addPathPatterns(new String[] {"/", "/**/*.do"});
}
@Bean
CryptographySupport cryptographySupport() {
Config config = Cryptography.Config.get();

@ -1,9 +1,7 @@
package cokr.xit.base.crypto;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
@ -11,10 +9,13 @@ import java.util.stream.Collectors;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ibatis.executor.parameter.ParameterHandler;
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 org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import cokr.xit.base.crypto.Cryptography.Config;
import cokr.xit.foundation.data.paging.MybatisPlugin;
@ -23,31 +24,12 @@ import cokr.xit.foundation.data.paging.MybatisPlugin;
* @author mjkhan
*/
@Intercepts({
@Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class}),
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class CryptographySupport extends MybatisPlugin {
private static final ThreadLocal<List<Object>> cached = new ThreadLocal<>();
private static boolean isCached(Object obj) {
List<Object> objs = cached.get();
return objs != null ? objs.contains(obj) : false;
}
private static void setCache(Object obj) {
List<Object> objs = cached.get();
if (objs == null)
cached.set(objs = new ArrayList<>());
objs.add(obj);
}
public static void clear() {
List<Object> objs = cached.get();
if (objs != null)
objs.clear();
cached.remove();
}
private Map<String, Cryptography> cryptos;
public CryptographySupport() {
@ -62,10 +44,8 @@ public class CryptographySupport extends MybatisPlugin {
}
@Override
protected Object setParameters(ParameterHandler parameterHandler, PreparedStatement statement) throws SQLException {
Object obj = parameterHandler.getParameterObject();
encrypt(obj);
return super.setParameters(parameterHandler, statement);
protected Object query(Executor executor, MappedStatement mappedStatement, Object obj, RowBounds rowBounds, ResultHandler<?> resultHandler) throws SQLException {
return encrypt(obj, () -> super.query(executor, mappedStatement, obj, rowBounds, resultHandler));
}
@Override
@ -82,24 +62,43 @@ public class CryptographySupport extends MybatisPlugin {
return obj;
}
@Override
protected Object update(Executor executor, MappedStatement mappedStatement, Object obj) throws SQLException {
return encrypt(obj, () -> super.update(executor, mappedStatement, obj));
}
private boolean isEnabled(Object obj) {
return !isEmpty(obj)
&& !isEmpty(cryptos);
}
private void encrypt(Object obj) {
if (!isEnabled(obj)) return;
private static interface ResultSupplier {
Object get() throws Exception;
}
private Object encrypt(Object obj, ResultSupplier resultSupplier) {
boolean enabled = isEnabled(obj);
if (enabled)
process(obj, (arg) -> convert(arg, (crypto, val) -> crypto.encrypt(val)));
process(obj, (arg) -> convert(arg, (crypto, val) -> crypto.encrypt(val), true));
Object result = null;
try {
result = resultSupplier.get();
} catch (Exception e) {
throw runtimeException(e);
}
private void convert(Object obj, BiFunction<Cryptography, Object, Object> converter, boolean cache) {
if (enabled)
process(obj, (arg) -> convert(arg, (crypto, val) -> crypto.decrypt(val)));
return result;
}
private void convert(Object obj, BiFunction<Cryptography, Object, Object> converter) {
List<Cryptography.TargetValue> targetValues = Cryptography.Config.get().getTargetValues(obj);
if (targetValues.isEmpty()) return;
try {
if (cache && isCached(obj)) return;
for (Cryptography.TargetValue target: targetValues) {
for (String cryptoDef: target.getCryptoDefs()) {
Cryptography crypto = cryptos.get(cryptoDef);
@ -127,9 +126,6 @@ public class CryptographySupport extends MybatisPlugin {
}
}
}
if (cache)
setCache(obj);
} catch (Exception e) {
throw runtimeException(e);
}
@ -138,6 +134,6 @@ public class CryptographySupport extends MybatisPlugin {
private void decrypt(Object obj) {
if (!isEnabled(obj)) return;
convert(obj, (crypto, val) -> crypto.decrypt(val), false);
convert(obj, (crypto, val) -> crypto.decrypt(val));
}
}

@ -1,22 +0,0 @@
package cokr.xit.base.crypto.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cokr.xit.base.crypto.CryptographySupport;
import cokr.xit.foundation.web.RequestInterceptor;
/** RequestInterceptor
* @author mjkhan
*/
public class CleanupCryptography extends RequestInterceptor {
@Override
public void afterCompletion(HttpServletRequest hreq, HttpServletResponse hresp, Object handler, Exception ex) throws Exception {
CryptographySupport.clear();
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest hreq, HttpServletResponse hresp, Object handler) throws Exception {
CryptographySupport.clear();
}
}
Loading…
Cancel
Save