Skip to content

Commit 0c7f332

Browse files
author
weasley
committed
Merge remote-tracking branch 'origin/main'
2 parents 4ffa641 + 857ca72 commit 0c7f332

File tree

11 files changed

+316
-7
lines changed

11 files changed

+316
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ alphahub:
6363

6464
## 2 Configure your configuration yaml file
6565

66-
[`MyBatis` encryption configuration](https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent/blob/main/mybatis-encrypt-spring-boot-starter/src/main/java/io/github/weasleyj/mybatis/encrypt/config/MybatisEncryptProperties.java),Embeded 2 algorithms.
66+
[`MyBatis` encryption configuration](https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent/blob/main/mybatis-encrypt-spring-boot-starter/src/main/java/io/github/weasleyj/mybatis/encrypt/config/MybatisEncryptProperties.java),2 algorithms embeded. you can choose one of them. Or you can implement you own algorithm.
6767

6868
### 2.1 Base64 algorithm
6969

@@ -150,7 +150,7 @@ public class SomeApplication {
150150

151151
## 3 Wirte a Java bean that MyBatis can map with table column
152152

153-
- Use `@Encryption` annotate on the field property which you need encryption and decryption,Here is a example to decrypt and encrypt `nickname`:
153+
- Use `@Encryption` annotate on the field property which you need encryption and decryption,Here is an example to decrypt and encrypt `nickname`:
154154

155155
[API link](https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent/blob/8500f7454d5ec150b5ece6549f4608b38183af19/mybatis-encrypt-spring-boot-tests/src/main/java/com/example/controller/MemberController.java#L112): http://localhost:8080/api/member/save/direct
156156

@@ -282,7 +282,7 @@ INSERT INTO dtt_member ( member_id, open_id, nickname, is_enable, balance, birth
282282

283283
- When sql with type of ` SELECT` Executed by `MyBatis` Executor, `nickname` will be decrypted, i.e:
284284

285-
[API link](https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent/blob/8500f7454d5ec150b5ece6549f4608b38183af19/mybatis-encrypt-spring-boot-tests/src/main/java/com/example/controller/MemberController.java#L53): http://localhost:8080/api/member/page/pagehelper?pageNum=1&pageSize=2
285+
[API link](https://github.com/Weasley-J/mybatis-encrypt-spring-boot-parent/blob/8500f7454d5ec150b5ece6549f4608b38183af19/mybatis-encrypt-spring-boot-tests/src/main/java/com/example/controller/MemberController.java#L53): http://localhost:8080/api/member/page/pagehelper?pageNum=1&pageSize=10
286286

287287
```log
288288
JDBC Connection [HikariProxyConnection@670142194 wrapping conn1: url=jdbc:h2:/Users/weasley/Development/IdeaProjects/mybatis-encrypt-spring-boot-parent/mybatis-encrypt-spring-boot-tests/h2 user=] will not be managed by Spring

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public String encrypt(Object plaintext) {
6161
}
6262
return encoded;
6363
} catch (Exception e) {
64-
throw new MybatisEncryptException("AEC encrypt exception:", e);
64+
throw new MybatisEncryptException("AES encrypt exception:", e);
6565
}
6666
}
6767

@@ -81,7 +81,7 @@ public String decrypt(Object ciphertext) {
8181
}
8282
return decrypted;
8383
} catch (Exception e) {
84-
throw new MybatisEncryptException("AEC decrypt exception:", e);
84+
throw new MybatisEncryptException("AES decrypt exception:", e);
8585
}
8686
}
8787
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ default String encrypt(Object plaintext) {
1818
}
1919

2020
/**
21-
* To decrypt an encrypted test that human-unreadable
21+
* To decrypt an encrypted text that human-unreadable
2222
*
2323
* @param ciphertext The encrypted text to decrypt
2424
* @return The decrypted text that human-readable
-8 KB
Binary file not shown.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@
9191
<version>4.2.2</version>
9292
</dependency>
9393
<!-- jdbc start -->
94+
<!-- oracle -->
95+
<dependency>
96+
<groupId>com.oracle.database.jdbc</groupId>
97+
<artifactId>ojdbc8</artifactId>
98+
<scope>runtime</scope>
99+
</dependency>
94100
<!-- mysql -->
95101
<dependency>
96102
<groupId>mysql</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.common.config;
2+
3+
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
4+
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
5+
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
6+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
7+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
8+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
import java.time.LocalDate;
15+
import java.time.LocalDateTime;
16+
import java.time.LocalTime;
17+
import java.time.format.DateTimeFormatter;
18+
import java.util.TimeZone;
19+
20+
/**
21+
* Spring Boot中LocalDateTime日期格式处理
22+
*
23+
* @author liuwenjing
24+
*/
25+
@Configuration
26+
public class Jackson2ObjectMapperConfig {
27+
/**
28+
* 日期时间格式,没有在yml里面配置默认采用: yyyy-MM-dd HH:mm:ss
29+
*/
30+
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
31+
private String pattern;
32+
/**
33+
* 时区,没有在yml里面配置默认采用: GMT+8
34+
*/
35+
@Value("${spring.jackson.time-zone:GMT+8}")
36+
private String timeZone;
37+
38+
/**
39+
* @return Jackson2ObjectMapperBuilderCustomizer
40+
*/
41+
@Bean
42+
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
43+
return builder -> {
44+
builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
45+
builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
46+
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)));
47+
48+
builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
49+
builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
50+
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern)));
51+
52+
builder.simpleDateFormat(pattern);
53+
builder.timeZone(TimeZone.getTimeZone(timeZone));
54+
};
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
logging:
2+
level:
3+
org.springframework.jdbc.core.JdbcTemplate: debug
4+
jdbc.sqltiming: debug
5+
6+
spring:
7+
datasource:
8+
driver-class-name: com.mysql.cj.jdbc.Driver
9+
url: jdbc:mysql://192.168.31.23:3306/db_demo?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true&allowMultiQueries=true
10+
username: root
11+
password: 123456
12+
13+
mybatis-plus:
14+
mapper-locations: classpath:mapper/**/*Mapper.xml,mapper/**/*Dao.xml
15+
type-aliases-package: com.example.domain.dtt,com.example.domain.order
16+
global-config:
17+
db-config:
18+
id-type: auto
19+
configuration:
20+
map-underscore-to-camel-case: true
21+
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
22+
23+
24+
# mybatis encryption configuration
25+
mybatis:
26+
encrypt:
27+
enable: on
28+
encrypt-type: aes
29+
aes:
30+
key: Jidkdp1mWL1tRyK=
31+
key-iv: Poikdp1mWL1jijK=
32+
33+
alphahub:
34+
dtt:
35+
is-enable: on
36+
banner-mode: ON
37+
show-sql: off
38+
mybatis-orm-support:
39+
is-enable: on
40+
all-in-one-table:
41+
enable: true
42+
filename: AllInOne.sql
43+
filepath: /Users/weasley/Downloads
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
logging:
2+
level:
3+
#cn.alphahub.dtt.plus.framework.core.template.DefaultTemplateResolver: debug
4+
org.springframework.jdbc.core.JdbcTemplate: debug
5+
jdbc.sqltiming: debug
6+
7+
spring:
8+
datasource:
9+
driver-class-name: oracle.jdbc.OracleDriver
10+
#driver-class-name: oracle.jdbc.driver.OracleDriver
11+
url: jdbc:oracle:thin:@//192.168.31.23:1521/lwj
12+
username: C##DTT
13+
password: 123456
14+
15+
mybatis-plus:
16+
mapper-locations: classpath:mapper/**/*Mapper.xml,mapper/**/*Dao.xml
17+
type-aliases-package: com.example.domain.dtt,com.example.domain.order
18+
global-config:
19+
db-config:
20+
id-type: auto
21+
configuration:
22+
map-underscore-to-camel-case: true
23+
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
24+
25+
26+
# mybatis encryption configuration
27+
mybatis:
28+
encrypt:
29+
enable: on
30+
encrypt-type: aes
31+
aes:
32+
key: Jidkdp1mWL1tRyK=
33+
key-iv: Poikdp1mWL1jijK=
34+
35+
alphahub:
36+
dtt:
37+
show-sql: on
38+
all-in-one-table:
39+
enable: true
40+
filename: AllInOne.sql
41+
filepath: /Users/weasley/Downloads

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: h2
13+
active: oracle
1414

1515
jackson:
1616
date-format: yyyy-MM-dd HH:mm:ss
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"pageNum": 1,
3+
"pageSize": 10,
4+
"total": 5,
5+
"pages": 1,
6+
"list": [
7+
{
8+
"memberId": 1,
9+
"openId": "fawezOE5sT",
10+
"nickname": "蒋震南",
11+
"isEnable": true,
12+
"balance": 865.0000,
13+
"birthday": "2022-12-20 01:58:00",
14+
"status": 0,
15+
"deleted": 1,
16+
"registrarDate": "2022-12-20",
17+
"accelerateBeginTime": "01:58:01",
18+
"accelerateEndTime": "01:58:01",
19+
"updateTime": "2022-12-20 01:58:00"
20+
},
21+
{
22+
"memberId": 2,
23+
"openId": "fawezOE5sT",
24+
"nickname": "蒋震南",
25+
"isEnable": true,
26+
"balance": 865.0000,
27+
"birthday": "2022-12-20 02:00:03",
28+
"status": 0,
29+
"deleted": 1,
30+
"registrarDate": "2022-12-20",
31+
"accelerateBeginTime": "02:00:03",
32+
"accelerateEndTime": "02:00:03",
33+
"updateTime": "2022-12-20 02:00:03"
34+
},
35+
{
36+
"memberId": 3,
37+
"openId": "fawezOE5sT",
38+
"nickname": "蒋震南",
39+
"isEnable": true,
40+
"balance": 865.0000,
41+
"birthday": "2022-12-20 02:00:04",
42+
"status": 0,
43+
"deleted": 1,
44+
"registrarDate": "2022-12-20",
45+
"accelerateBeginTime": "02:00:04",
46+
"accelerateEndTime": "02:00:04",
47+
"updateTime": "2022-12-20 02:00:04"
48+
},
49+
{
50+
"memberId": 4,
51+
"openId": "fawezOE5sT",
52+
"nickname": "蒋震南",
53+
"isEnable": true,
54+
"balance": 865.0000,
55+
"birthday": "2022-12-20 02:00:04",
56+
"status": 0,
57+
"deleted": 1,
58+
"registrarDate": "2022-12-20",
59+
"accelerateBeginTime": "02:00:05",
60+
"accelerateEndTime": "02:00:05",
61+
"updateTime": "2022-12-20 02:00:04"
62+
},
63+
{
64+
"memberId": 5,
65+
"openId": "fawezOE5sT",
66+
"nickname": "蒋震南",
67+
"isEnable": true,
68+
"balance": 865.0000,
69+
"birthday": "2022-12-20 02:00:06",
70+
"status": 0,
71+
"deleted": 1,
72+
"registrarDate": "2022-12-20",
73+
"accelerateBeginTime": "02:00:06",
74+
"accelerateEndTime": "02:00:06",
75+
"updateTime": "2022-12-20 02:00:06"
76+
}
77+
]
78+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,98 @@
11
package com.example;
22

3+
import cn.alphahub.dtt.plus.util.JacksonUtil;
4+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5+
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
6+
import com.example.common.page.PageWrapper;
7+
import com.example.demain.DttMember;
8+
import com.example.service.MemberService;
9+
import com.fasterxml.jackson.core.type.TypeReference;
10+
import org.apache.commons.io.IOUtils;
11+
import org.apache.commons.lang3.RandomStringUtils;
312
import org.junit.jupiter.api.Test;
13+
import org.springframework.beans.factory.annotation.Autowired;
414
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.util.Assert;
16+
17+
import java.io.IOException;
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.ArrayList;
20+
import java.util.List;
521

622
@SpringBootTest
723
class EncryptionTestsAppTests {
824

25+
@Autowired
26+
private MemberService memberService;
27+
928
@Test
1029
void contextLoads() {
1130
}
1231

32+
@Test
33+
void testBatchInsert() throws IOException {
34+
String resource = IOUtils.resourceToString("member.json", StandardCharsets.UTF_8, Thread.currentThread().getContextClassLoader());
35+
PageWrapper<DttMember> pageWrapper = JacksonUtil.readValue(resource, new TypeReference<PageWrapper<DttMember>>() {
36+
});
37+
38+
List<DttMember> list = pageWrapper.getList();
39+
List<DttMember> insertList = new ArrayList<>();
40+
int i = 1000;
41+
Long id = null;
42+
43+
DttMember one = memberService.getOne(new QueryWrapper<DttMember>().select("MAX(member_id) memberId"));
44+
if (null != one) {
45+
id = one.getMemberId();
46+
} else id = 1L;
47+
48+
for (DttMember dttMember : list) {
49+
i += 1;
50+
id += 1;
51+
dttMember.setOpenId(RandomStringUtils.randomAlphanumeric(16));
52+
dttMember.setMemberId(id + 1);
53+
dttMember.setNickname(dttMember.getNickname() + i);
54+
insertList.add(dttMember);
55+
}
56+
57+
boolean batch = memberService.saveBatch(insertList);
58+
Assert.isTrue(batch);
59+
}
60+
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+
70+
@Test
71+
void testUpdateSingle() {
72+
DttMember member = JacksonUtil.readValue("{\n" +
73+
" \"memberId\": 3,\n" +
74+
" \"openId\": \"fawezOE5sT\",\n" +
75+
" \"nickname\": \"蒋震南1001\",\n" +
76+
" \"isEnable\": true,\n" +
77+
" \"balance\": 865.0000,\n" +
78+
" \"birthday\": \"2022-12-20 01:58:00\",\n" +
79+
" \"status\": 0,\n" +
80+
" \"deleted\": 1,\n" +
81+
" \"registrarDate\": \"2022-12-20\",\n" +
82+
" \"accelerateBeginTime\": \"01:58:01\",\n" +
83+
" \"accelerateEndTime\": \"01:58:01\",\n" +
84+
" \"updateTime\": \"2022-12-20 01:58:00\"\n" +
85+
" }", DttMember.class);
86+
member.setOpenId(RandomStringUtils.randomAlphanumeric(16));
87+
boolean update = this.memberService.update(member, Wrappers.lambdaUpdate(DttMember.class)
88+
.eq(DttMember::getMemberId, 3)
89+
);
90+
Assert.isTrue(update, "update must be success");
91+
92+
boolean update2 = this.memberService.update(null, Wrappers.lambdaUpdate(DttMember.class)
93+
.eq(DttMember::getMemberId, 3)
94+
.set(DttMember::getIsEnable, false)
95+
);
96+
Assert.isTrue(update2, "update must be success");
97+
}
1398
}

0 commit comments

Comments
 (0)