Skip to content

Commit c779072

Browse files
author
Weasley
committed
- make a solution for using encrypted fields as query criteria
1 parent 857ca72 commit c779072

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed

mybatis-encrypt-spring-boot-starter/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<artifactId>mybatis-encrypt-spring-boot-starter</artifactId>
1414
<version>1.0.0</version>
1515
<name>mybatis-encrypt-spring-boot-starter</name>
16-
<description>A spring-boot starter make it easy to encrypt and decrypt some column of database tables, support for user custom encryption algorithms</description>
16+
<description>A spring-boot starter make it easy to encrypt and decrypt some column of database tables, support for
17+
user custom encryption algorithms
18+
</description>
1719
<url>https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent</url>
1820

1921
<licenses>

mybatis-encrypt-spring-boot-starter/src/main/java/io/github/weasleyj/mybatis/encrypt/config/MybatisEncryptConfigurer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MybatisEncryptConfigurer implements InitializingBean {
4646
/**
4747
* The clients of encrypt strategies
4848
*/
49-
public static final Map<EncryptType, EncryptStrategy> STRATEGIES_CLIENTS = new ConcurrentHashMap<>(6);
49+
public static final Map<EncryptType, EncryptStrategy> STRATEGY_CLIENTS = new ConcurrentHashMap<>(6);
5050
private final Logger logger = LoggerFactory.getLogger(this.getClass());
5151
private final DiyProperties diyProperties;
5252
private final AesProperties aesProperties;
@@ -63,7 +63,6 @@ public MybatisEncryptConfigurer(DiyProperties diyProperties, AesProperties aesPr
6363
this.defaultMybatisEncryptInterceptor = defaultMybatisEncryptInterceptor;
6464
}
6565

66-
6766
@Override
6867
public void afterPropertiesSet() throws Exception {
6968
sqlSessionFactories.forEach(sqlSessionFactory -> {
@@ -73,9 +72,9 @@ public void afterPropertiesSet() throws Exception {
7372
}
7473
});
7574
if (null != diyProperties.getEncryptStrategy()) {
76-
STRATEGIES_CLIENTS.put(DIY, ClassUtils.newInstance(diyProperties.getEncryptStrategy()));
75+
STRATEGY_CLIENTS.put(DIY, ClassUtils.newInstance(diyProperties.getEncryptStrategy()));
7776
}
78-
STRATEGIES_CLIENTS.put(BASE64, new DefaultBase64EncryptStrategyImpl());
79-
STRATEGIES_CLIENTS.put(AES, new DefaultAesEncryptStrategyImpl(aesProperties));
77+
STRATEGY_CLIENTS.put(BASE64, new DefaultBase64EncryptStrategyImpl());
78+
STRATEGY_CLIENTS.put(AES, new DefaultAesEncryptStrategyImpl(aesProperties));
8079
}
8180
}

mybatis-encrypt-spring-boot-starter/src/main/java/io/github/weasleyj/mybatis/encrypt/core/EncryptStrategy.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
package io.github.weasleyj.mybatis.encrypt.core;
22

3+
import io.github.weasleyj.mybatis.encrypt.annotation.Encryption;
4+
import io.github.weasleyj.mybatis.encrypt.constant.EncryptType;
5+
import io.github.weasleyj.mybatis.encrypt.exception.MybatisEncryptException;
6+
import org.apache.commons.lang3.reflect.FieldUtils;
7+
import org.springframework.util.Assert;
8+
import org.springframework.util.CollectionUtils;
9+
10+
import java.lang.reflect.Field;
11+
import java.util.List;
12+
13+
import static io.github.weasleyj.mybatis.encrypt.config.MybatisEncryptConfigurer.STRATEGY_CLIENTS;
14+
315
/**
416
* The interface for encrypt strategy
517
*
618
* @author weasley
719
* @version 1.0.0
820
*/
921
public interface EncryptStrategy {
22+
/**
23+
* When using encrypted fields as query fields, you may need to encrypt plaintext fields before they can be recognized by the database as query parameters
24+
*
25+
* @param plainBean The bean to encrypt its fields to ciphertext which annotated by {@link Encryption}
26+
* @param <E> The type of raw bean
27+
* @return The bean with fields encrypt to ciphertext
28+
*/
29+
static <E> E covert(E plainBean, EncryptType encryptType) {
30+
Assert.notNull(plainBean, "Plain bean must be not null");
31+
Assert.notNull(encryptType, "Encrypt type must be not null");
32+
if (plainBean.getClass() == Object.class) return plainBean;
33+
List<Field> encryptionFields = FieldUtils.getFieldsListWithAnnotation(plainBean.getClass(), Encryption.class);
34+
if (CollectionUtils.isEmpty(encryptionFields)) return plainBean;
35+
EncryptStrategy encryptStrategy = STRATEGY_CLIENTS.get(encryptType);
36+
if (null == encryptStrategy) return plainBean;
37+
for (Field field : encryptionFields) {
38+
try {
39+
Object plaintextFieldValue = FieldUtils.readField(field, plainBean, true);
40+
if (null == plaintextFieldValue) continue;
41+
String encrypt = encryptStrategy.encrypt(plaintextFieldValue);
42+
FieldUtils.writeField(field, plainBean, encrypt, true);
43+
} catch (IllegalAccessException e) {
44+
throw new MybatisEncryptException("Covert plain bean to cipher bean error:", e);
45+
}
46+
}
47+
return plainBean;
48+
}
49+
1050
/**
1151
* To encrypt a raw text that human-readable
1252
*
@@ -26,5 +66,4 @@ default String encrypt(Object plaintext) {
2666
default String decrypt(Object ciphertext) {
2767
return null;
2868
}
29-
3069
}

mybatis-encrypt-spring-boot-starter/src/main/java/io/github/weasleyj/mybatis/encrypt/interceptor/DefaultMybatisEncryptInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public Object processSelectCommandType(Invocation invocation) throws IllegalAcce
143143
* @return The instance of {@link EncryptStrategy}
144144
*/
145145
public EncryptStrategy deduceEncryptStrategy() {
146-
return MybatisEncryptConfigurer.STRATEGIES_CLIENTS.get(mybatisEncryptProperties.getEncryptType());
146+
return MybatisEncryptConfigurer.STRATEGY_CLIENTS.get(mybatisEncryptProperties.getEncryptType());
147147
}
148148

149149
}
8 KB
Binary file not shown.

mybatis-encrypt-spring-boot-tests/src/main/java/com/example/controller/MemberController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.example.demain.DttMember;
1111
import com.example.service.MemberService;
1212
import com.github.pagehelper.page.PageMethod;
13+
import io.github.weasleyj.mybatis.encrypt.config.MybatisEncryptProperties;
14+
import io.github.weasleyj.mybatis.encrypt.core.EncryptStrategy;
1315
import lombok.extern.slf4j.Slf4j;
1416
import org.springframework.beans.factory.annotation.Autowired;
1517
import org.springframework.http.ResponseEntity;
@@ -35,6 +37,8 @@ public class MemberController {
3537

3638
@Autowired
3739
private MemberService memberService;
40+
@Autowired
41+
private MybatisEncryptProperties mybatisEncryptProperties;
3842

3943
/**
4044
* 用户信息分页查询(Pagehelper写法)
@@ -77,6 +81,19 @@ public ResponseEntity<Page<DttMember>> pageByMmp(@ModelAttribute("pageParam") Pa
7781
return ResponseEntity.ok(page);
7882
}
7983

84+
/**
85+
* Use encrypted fields as query criteria
86+
*/
87+
@GetMapping("/lis/encrypted/fields")
88+
public ResponseEntity<List<DttMember>> selectByEncryptedFields(@ModelAttribute("member") DttMember member) {
89+
log.info("{}", JacksonUtil.toPrettyJson(member));
90+
DttMember dttMember = EncryptStrategy.covert(member, mybatisEncryptProperties.getEncryptType());
91+
log.info("EncryptStrategy.covert {}", JacksonUtil.toPrettyJson(dttMember));
92+
List<DttMember> members = this.memberService.list(Wrappers.lambdaQuery(DttMember.class)
93+
.eq(DttMember::getNickname, dttMember.getNickname()));
94+
return ResponseEntity.ok(members);
95+
}
96+
8097
/**
8198
* 获取用户信息详情
8299
*

mybatis-encrypt-spring-boot-tests/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ spring:
1010
name: mybatis-encrypt-spring-boot-tests
1111

1212
profiles:
13-
active: oracle
13+
active: h2
1414

1515
jackson:
1616
date-format: yyyy-MM-dd HH:mm:ss

mybatis-encrypt-spring-boot-tests/src/test/java/com/example/EncryptionTestsAppTests.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.example.demain.DttMember;
88
import com.example.service.MemberService;
99
import com.fasterxml.jackson.core.type.TypeReference;
10+
import io.github.weasleyj.mybatis.encrypt.config.MybatisEncryptProperties;
11+
import io.github.weasleyj.mybatis.encrypt.core.EncryptStrategy;
12+
import lombok.extern.slf4j.Slf4j;
1013
import org.apache.commons.io.IOUtils;
1114
import org.apache.commons.lang3.RandomStringUtils;
1215
import org.junit.jupiter.api.Test;
@@ -19,11 +22,14 @@
1922
import java.util.ArrayList;
2023
import java.util.List;
2124

25+
@Slf4j
2226
@SpringBootTest
2327
class EncryptionTestsAppTests {
2428

2529
@Autowired
2630
private MemberService memberService;
31+
@Autowired
32+
private MybatisEncryptProperties mybatisEncryptProperties;
2733

2834
@Test
2935
void contextLoads() {
@@ -58,15 +64,6 @@ void testBatchInsert() throws IOException {
5864
Assert.isTrue(batch);
5965
}
6066

61-
@Test
62-
void testSelectByEncryptFiled() {
63-
List<DttMember> members = this.memberService.list(Wrappers.lambdaQuery(DttMember.class)
64-
.eq(DttMember::getNickname, "蒋震南1005")
65-
.eq(DttMember::getOpenId, "fawezOE5sT")
66-
);
67-
System.out.println(JacksonUtil.toPrettyJson(members));
68-
}
69-
7067
@Test
7168
void testUpdateSingle() {
7269
DttMember member = JacksonUtil.readValue("{\n" +
@@ -95,4 +92,20 @@ void testUpdateSingle() {
9592
);
9693
Assert.isTrue(update2, "update must be success");
9794
}
95+
96+
97+
/**
98+
* Use an encrypted field to query for data
99+
*/
100+
@Test
101+
void testSelectByEncryptedFields() {
102+
DttMember member = new DttMember().setNickname("蒋震南1005");
103+
log.info("before {}", JacksonUtil.toJson(member));
104+
DttMember dttMember = EncryptStrategy.covert(member, mybatisEncryptProperties.getEncryptType());
105+
log.info("after {}", JacksonUtil.toJson(member));
106+
List<DttMember> members = this.memberService.list(Wrappers.lambdaQuery(DttMember.class)
107+
.eq(DttMember::getNickname, dttMember.getNickname()));
108+
log.info("select by encrypt filed: {}", JacksonUtil.toJson(members));
109+
}
110+
98111
}

0 commit comments

Comments
 (0)