From d8f9cd21c435956ed517b2fa79df3035cfa00ce8 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Tue, 5 Dec 2023 18:45:28 +0900 Subject: [PATCH] =?UTF-8?q?key,=20algorithm=20=EB=94=94=ED=8F=B4=ED=8A=B8?= =?UTF-8?q?=EA=B0=92=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cokr/xit/foundation/data/ARIA.java | 84 ++++++++++++- .../foundation/data/paging/MybatisPlugin.java | 113 ++++++++++++++++++ 2 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cokr/xit/foundation/data/paging/MybatisPlugin.java diff --git a/src/main/java/cokr/xit/foundation/data/ARIA.java b/src/main/java/cokr/xit/foundation/data/ARIA.java index f291da9..96abdeb 100644 --- a/src/main/java/cokr/xit/foundation/data/ARIA.java +++ b/src/main/java/cokr/xit/foundation/data/ARIA.java @@ -1,6 +1,9 @@ package cokr.xit.foundation.data; import java.util.Base64; +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Function; import org.egovframe.rte.fdl.cryptography.EgovPasswordEncoder; import org.egovframe.rte.fdl.cryptography.impl.EgovARIACryptoServiceImpl; @@ -23,7 +26,14 @@ public class ARIA extends AbstractComponent { * @return 키 */ public String getKey() { - return key; + if (!isEmpty(key)) + return key; + + if (properties != null) { + return key = properties.getString("aria.key"); + } else { + return key; + } } /**키를 설정한다. @@ -39,7 +49,14 @@ public class ARIA extends AbstractComponent { * @return 알고리즘 */ public String getAlgorithm() { - return algorithm; + if (!isEmpty(algorithm)) + return algorithm; + + if (properties != null) { + return algorithm = properties.getString("aria.algorithm"); + } else { + return algorithm; + } } /**알고리즘을 설정한다. 디폴트는 SHA-256. @@ -78,6 +95,42 @@ public class ARIA extends AbstractComponent { return Base64.getEncoder().encodeToString(bytes); } + public void encrypt(Function getter, BiConsumer setter, Iterable objs) { + convert( + obj -> { + String str = getter.apply(obj); + return encrypt(str); + }, + setter, + objs + ); + } + + public void encrypt(Function getter, BiConsumer setter, T... objs) { + encrypt(getter, setter, List.of(objs)); + } + + public void encrypt(String fieldName, Iterable rows) { + encrypt( + row -> row.string(fieldName), + (row, str) -> row.set(fieldName, str), + rows + ); + } + + public void encrypt(String fieldName, DataObject... rows) { + encrypt(fieldName, List.of(rows)); + } + + private void convert(Function converter, BiConsumer setter, Iterable objs) { + if (isEmpty(objs)) return; + + objs.forEach(obj -> { + String str = converter.apply(obj); + setter.accept(obj, str); + }); + } + private EgovPasswordEncoder passwordEncoder() { if (passwordEncoder == null) { passwordEncoder = new EgovPasswordEncoder(); @@ -107,6 +160,33 @@ public class ARIA extends AbstractComponent { return new String(service().decrypt(bytes, key)); } + public void decrypt(Function getter, BiConsumer setter, Iterable objs) { + convert( + obj -> { + String str = getter.apply(obj); + return decrypt(str); + }, + setter, + objs + ); + } + + public void decrypt(Function getter, BiConsumer setter, T... objs) { + decrypt(getter, setter, List.of(objs)); + } + + public void decrypt(String fieldName, Iterable rows) { + decrypt( + row -> row.string(fieldName), + (row, str) -> row.set(fieldName, str), + rows + ); + } + + public void decrypt(String fieldName, DataObject... rows) { + decrypt(fieldName, List.of(rows)); + } + /**현재 설정상태를 비운다. * @return 현재 ARIA */ diff --git a/src/main/java/cokr/xit/foundation/data/paging/MybatisPlugin.java b/src/main/java/cokr/xit/foundation/data/paging/MybatisPlugin.java new file mode 100644 index 0000000..7ac0959 --- /dev/null +++ b/src/main/java/cokr/xit/foundation/data/paging/MybatisPlugin.java @@ -0,0 +1,113 @@ +package cokr.xit.foundation.data.paging; + +import java.lang.reflect.Method; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.parameter.ParameterHandler; +import org.apache.ibatis.executor.resultset.ResultSetHandler; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import cokr.xit.foundation.AbstractComponent; + +/**조회 결과의 페이징 처리를 지원하는 MyBatis Interceptor + * @author mjkhan + */ +@Intercepts({ + @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}), + @Signature(type = StatementHandler.class, method = "parameterize", args = {Statement.class}), + @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}), + @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}), + + @Signature(type = ParameterHandler.class, method = "getParameterObject", args = {}), + @Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class}), + + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), + + @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) +}) +public class MybatisPlugin extends AbstractComponent implements Interceptor { + @Override + public Object intercept(Invocation invocation) throws Throwable { + Object target = invocation.getTarget(); + Method method = invocation.getMethod(); + Object[] args = invocation.getArgs(); + + String methodName = method.toString(); + System.out.println(methodName); + + if (methodName.contains("Executor.query")) + return query((Executor)target, (MappedStatement)args[0], args[1], (RowBounds)args[2], (ResultHandler)args[3]); + else if (methodName.contains("Executor.update")) + return update((Executor)target, (MappedStatement)args[0], args[1]); + + else if (methodName.contains("StatementHandler.prepare")) + return prepare((StatementHandler)target, (Connection)args[0], (Integer)args[1]); + else if (methodName.contains("StatementHandler.parameterize")) + return parameterize((StatementHandler)target, (Statement)args[0]); + else if (methodName.contains("StatementHandler.query")) + return query((StatementHandler)target, (Statement)args[0], (ResultHandler)args[1]); + else if (methodName.contains("StatementHandler.update")) + return update((StatementHandler)target, (Statement)args[0]); + + else if (methodName.contains("ParameterHandler.getParameterObject")) + return getParameterObject((ParameterHandler)target); + else if (methodName.contains("ParameterHandler.setParameters")) + return setParameters((ParameterHandler)target, (PreparedStatement)args[0]); + + else if (methodName.contains("ResultSetHandler.handleResultSets")) + return handle((ResultSetHandler)target, (Statement)args[0]); + + else + return invocation.proceed(); + } + + protected Object query(Executor executor, MappedStatement mappedStatement, Object obj, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { + return executor.query(mappedStatement, obj, rowBounds, resultHandler); + } + + protected Object update(Executor executor, MappedStatement mappedStatement, Object obj) throws SQLException { + return executor.update(mappedStatement, obj); + } + + protected Object prepare(StatementHandler statementHandler, Connection connection, Integer timeout) throws SQLException { + return statementHandler.prepare(connection, timeout); + } + + protected Object parameterize(StatementHandler statementHandler, Statement statement) throws SQLException { + statementHandler.parameterize(statement); + return null; + } + + protected Object getParameterObject(ParameterHandler parameterHandler) { + return parameterHandler.getParameterObject(); + } + + protected Object setParameters(ParameterHandler parameterHandler, PreparedStatement statement) throws SQLException { + parameterHandler.setParameters(statement); + return null; + } + + protected Object query(StatementHandler statementHandler, Statement statement, ResultHandler resultHandler) throws SQLException { + return statementHandler.query(statement, resultHandler); + } + + protected int update(StatementHandler statementHandler, Statement statement) throws SQLException { + return statementHandler.update(statement); + } + + protected Object handle(ResultSetHandler resultSetHandler, Statement statement) throws SQLException { + return resultSetHandler.handleResultSets(statement); + } +} \ No newline at end of file