Skip to content

Commit

Permalink
Fix issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteDG committed Mar 13, 2023
1 parent 56a9a26 commit acef2a0
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.whitedg.mybatis.crypto;

import com.esotericsoftware.kryo.kryo5.Kryo;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
Expand Down Expand Up @@ -39,88 +38,49 @@ public Object intercept(Invocation invocation) throws Throwable {
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
if (Util.encryptionRequired(parameter, ms.getSqlCommandType())) {
boolean isParamMap = parameter instanceof MapperMethod.ParamMap;
if (isParamMap) {
//noinspection unchecked
MapperMethod.ParamMap<Object> paramMap = (MapperMethod.ParamMap<Object>) parameter;
encryptParamMap(paramMap);
} else {
Object execParam = encryptEntity(parameter);
args[1] = execParam;
}
doEncrypt(parameter);
Object result = invocation.proceed();
postExecution(ms, parameter, args[1], isParamMap);
if (keepParameter) {
doDecrypt(parameter);
}
return result;
} else {
return invocation.proceed();
}
}

private void postExecution(MappedStatement ms, Object parameter, Object execParam, boolean isParamMap) throws IllegalAccessException {
if (!keepParameter) {
return;
}
if (!isParamMap) {
handleKeyProperties(ms, parameter, execParam);
} else {
//noinspection unchecked
MapperMethod.ParamMap<Object> plainParamMap = (MapperMethod.ParamMap<Object>) parameter;
//noinspection unchecked
MapperMethod.ParamMap<Object> cipherParamMap = (MapperMethod.ParamMap<Object>) execParam;
for (Map.Entry<String, Object> plainEntry : plainParamMap.entrySet()) {
String key = plainEntry.getKey();
for (String keyPrefix : mappedKeyPrefixes) {
if (key != null && key.startsWith(keyPrefix)) {
Object plainVal = plainEntry.getValue();
Object cipherVal = cipherParamMap.get(key);
if (plainVal instanceof ArrayList) {
//noinspection rawtypes
ArrayList plainList = (ArrayList) plainVal;
//noinspection rawtypes
ArrayList cipherList = (ArrayList) cipherVal;
for (int i = 0; i < plainList.size(); i++) {
handleKeyProperties(ms, plainList.get(i), cipherList.get(i));
}
} else {
handleKeyProperties(ms, plainVal, cipherVal);
}
}
}
}
}
private void doEncrypt(Object parameter) {
handleParameter(Mode.ENCRYPT, parameter);
}

private void handleKeyProperties(MappedStatement ms, Object parameter, Object copyOfParameter) throws IllegalAccessException {
List<Field> keyFields = KeyFieldsProvider.get(ms, copyOfParameter);
for (Field keyField : keyFields) {
keyField.set(parameter, keyField.get(copyOfParameter));
private void doDecrypt(Object parameter) {
handleParameter(Mode.DECRYPT, parameter);
}

private void handleParameter(Mode mode, Object parameter) {
boolean isParamMap = parameter instanceof MapperMethod.ParamMap;
if (isParamMap) {
//noinspection unchecked
MapperMethod.ParamMap<Object> paramMap = (MapperMethod.ParamMap<Object>) parameter;
encryptParamMap(mode, paramMap);
} else {
encryptEntity(mode, parameter);
}
}

private <T> T encryptEntity(T parameter) throws MybatisCryptoException {
private <T> void encryptEntity(Mode mode, T parameter) throws MybatisCryptoException {
Set<Field> encryptedFields = EncryptedFieldsProvider.get(parameter.getClass());
if (encryptedFields == null || encryptedFields.isEmpty()) {
return parameter;
}
T execParam = parameter;
Kryo kryo = null;
try {
if (keepParameter) {
kryo = KryoPool.obtain();
execParam = kryo.copy(parameter);
}
processFields(encryptedFields, execParam);
return execParam;
} finally {
KryoPool.free(kryo);
return;
}
processFields(mode, encryptedFields, parameter);
}

private void encryptParamMap(MapperMethod.ParamMap<Object> paramMap) throws MybatisCryptoException {
Set<Map.Entry<String, Object>> entrySet = paramMap.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
private void encryptParamMap(Mode mode, MapperMethod.ParamMap<Object> paramMap) throws MybatisCryptoException {
Set<Map.Entry<String, Object>> paramMapEntrySet = paramMap.entrySet();
for (Map.Entry<String, Object> paramEntry : paramMapEntrySet) {
String key = paramEntry.getKey();
Object value = paramEntry.getValue();
if (value == null || key == null) {
continue;
}
Expand All @@ -134,18 +94,18 @@ private void encryptParamMap(MapperMethod.ParamMap<Object> paramMap) throws Myba
Class<?> itemClass = firstItem.getClass();
Set<Field> encryptedFields = EncryptedFieldsProvider.get(itemClass);
for (Object item : list) {
processFields(encryptedFields, item);
processFields(mode, encryptedFields, item);
}
}
} else {
processFields(EncryptedFieldsProvider.get(value.getClass()), value);
processFields(mode, EncryptedFieldsProvider.get(value.getClass()), value);
}
}
}
}
}

private void processFields(Set<Field> encryptedFields, Object entry) throws MybatisCryptoException {
private void processFields(Mode mode, Set<Field> encryptedFields, Object entry) throws MybatisCryptoException {
if (encryptedFields == null || encryptedFields.isEmpty()) {
return;
}
Expand All @@ -164,8 +124,8 @@ private void processFields(Set<Field> encryptedFields, Object entry) throws Myba
}
String key = Util.getKeyOrDefault(encryptedField, defaultKey);
IEncryptor iEncryptor = EncryptorProvider.getOrDefault(encryptedField, defaultEncryptor);
String encryptedVal = iEncryptor.encrypt(originalVal, key);
field.set(entry, encryptedVal);
String updatedVal = Mode.ENCRYPT.equals(mode) ? iEncryptor.encrypt(originalVal, key) : iEncryptor.decrypt(originalVal, key);
field.set(entry, updatedVal);
} catch (Exception e) {
if (failFast) {
throw new MybatisCryptoException(e);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.whitedg.mybatis.crypto;

/**
* @author White
*/
enum Mode {
ENCRYPT, DECRYPT
}
6 changes: 5 additions & 1 deletion mybatis-crypto-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>io.github.whitedg</groupId>
<artifactId>mybatis-crypto-spring-boot-starter</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>io.github.whitedg</groupId>
Expand All @@ -62,6 +62,10 @@
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

</dependencies>

Expand Down
12 changes: 12 additions & 0 deletions mybatis-crypto-demo/src/main/resources/application-h2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring:
datasource:
url: jdbc:h2:mem:demo_db
driver-class-name: org.h2.Driver
username: sa
password: sa
h2:
console:
enabled: true
sql:
init:
schema-locations: classpath:db/h2/schema.sql
8 changes: 8 additions & 0 deletions mybatis-crypto-demo/src/main/resources/application-mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
datasource:
username: root
password: ne2FRwzOQjONzEOmx0oZ4uh9
hikari:
jdbc-url: jdbc:mysql://localhost:3306/mybatis_crypto
driver-class-name: com.mysql.cj.jdbc.Driver

13 changes: 2 additions & 11 deletions mybatis-crypto-demo/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@ server:
port: 7070

spring:
datasource:
url: jdbc:h2:mem:demo_db
driver-class-name: org.h2.Driver
username: sa
password: sa
h2:
console:
enabled: true
sql:
init:
schema-locations: classpath:db/schema.sql
profiles:
active: h2

mybatis-crypto:
enabled: true
Expand Down
15 changes: 15 additions & 0 deletions mybatis-crypto-demo/src/main/resources/db.mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE
DATABASE `mybatis_crypto` CHARACTER
SET utf8mb4 COLLATE utf8mb4_general_ci;

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user`
(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT NULL,
`email` VARCHAR(255) NULL DEFAULT NULL,
`password` VARCHAR(255) NULL DEFAULT NULL,
`id_card_no` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</modules>

<properties>
<revision>1.2.1</revision>
<revision>1.2.2</revision>

<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
Expand Down

0 comments on commit acef2a0

Please sign in to comment.