Commit ce3aefadd70b70516fd664bfcd580d67914276a8

Authored by YunaiV
1 parent 3fbb0b4c

移除 jasypt-spring-boot-starter 依赖,使用 hutool AES 替代

yudao-dependencies/pom.xml
... ... @@ -43,7 +43,6 @@
43 43 <!-- Bpm 工作流相关 -->
44 44 <flowable.version>6.7.2</flowable.version>
45 45 <!-- 工具类相关 -->
46   - <jasypt-spring-boot-starter.version>3.0.4</jasypt-spring-boot-starter.version>
47 46 <lombok.version>1.18.24</lombok.version>
48 47 <mapstruct.version>1.5.3.Final</mapstruct.version>
49 48 <hutool.version>5.8.9</hutool.version>
... ... @@ -408,12 +407,6 @@
408 407 </dependency>
409 408  
410 409 <dependency>
411   - <groupId>com.github.ulisesbocchio</groupId>
412   - <artifactId>jasypt-spring-boot-starter</artifactId> <!-- 加解密 -->
413   - <version>${jasypt-spring-boot-starter.version}</version>
414   - </dependency>
415   -
416   - <dependency>
417 410 <groupId>cn.iocoder.boot</groupId>
418 411 <artifactId>yudao-spring-boot-starter-excel</artifactId>
419 412 <version>${revision}</version>
... ...
yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml
... ... @@ -59,13 +59,6 @@
59 59 <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <!-- 多数据源 -->
60 60 </dependency>
61 61  
62   - <!-- 工具类相关 -->
63   - <dependency>
64   - <groupId>com.github.ulisesbocchio</groupId>
65   - <artifactId>jasypt-spring-boot-starter</artifactId> <!-- 加解密 -->
66   - <optional>true</optional>
67   - </dependency>
68   -
69 62 </dependencies>
70 63  
71 64 </project>
... ...
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/type/EncryptTypeHandler.java
1 1 package cn.iocoder.yudao.framework.mybatis.core.type;
2 2  
3 3 import cn.hutool.core.lang.Assert;
  4 +import cn.hutool.crypto.SecureUtil;
  5 +import cn.hutool.crypto.symmetric.AES;
4 6 import cn.hutool.extra.spring.SpringUtil;
5 7 import org.apache.ibatis.type.BaseTypeHandler;
6 8 import org.apache.ibatis.type.JdbcType;
7   -import org.jasypt.encryption.StringEncryptor;
8 9  
9 10 import java.sql.CallableStatement;
10 11 import java.sql.PreparedStatement;
... ... @@ -12,18 +13,20 @@ import java.sql.ResultSet;
12 13 import java.sql.SQLException;
13 14  
14 15 /**
15   - * 字段字段的 TypeHandler 实现类,基于 {@link StringEncryptor} 实现
  16 + * 字段字段的 TypeHandler 实现类,基于 {@link cn.hutool.crypto.symmetric.AES} 实现
16 17 * 可通过 jasypt.encryptor.password 配置项,设置密钥
17 18 *
18 19 * @author 芋道源码
19 20 */
20 21 public class EncryptTypeHandler extends BaseTypeHandler<String> {
21 22  
22   - private static StringEncryptor encryptor;
  23 + private static final String ENCRYPTOR_PROPERTY_NAME = "mybatis-plus.encryptor.password";
  24 +
  25 + private static AES aes;
23 26  
24 27 @Override
25 28 public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
26   - ps.setString(i, getEncryptor().encrypt(parameter));
  29 + ps.setString(i, encrypt(parameter));
27 30 }
28 31  
29 32 @Override
... ... @@ -48,23 +51,25 @@ public class EncryptTypeHandler extends BaseTypeHandler&lt;String&gt; {
48 51 if (value == null) {
49 52 return null;
50 53 }
51   - return getEncryptor().decrypt(value);
  54 + return getEncryptor().decryptStr(value);
52 55 }
53 56  
54 57 public static String encrypt(String rawValue) {
55 58 if (rawValue == null) {
56 59 return null;
57 60 }
58   - return getEncryptor().encrypt(rawValue);
  61 + return getEncryptor().encryptBase64(rawValue);
59 62 }
60 63  
61   - private static StringEncryptor getEncryptor() {
62   - if (encryptor != null) {
63   - return encryptor;
  64 + private static AES getEncryptor() {
  65 + if (aes != null) {
  66 + return aes;
64 67 }
65   - encryptor = SpringUtil.getBean(StringEncryptor.class);
66   - Assert.notNull(encryptor, "StringEncryptor 不能为空");
67   - return encryptor;
  68 + // 构建 AES
  69 + String password = SpringUtil.getProperty(ENCRYPTOR_PROPERTY_NAME);
  70 + Assert.notEmpty(password, "配置项({}) 不能为空", ENCRYPTOR_PROPERTY_NAME);
  71 + aes = SecureUtil.aes(password.getBytes());
  72 + return aes;
68 73 }
69 74  
70 75 }
... ...
yudao-module-infra/yudao-module-infra-biz/pom.xml
... ... @@ -83,10 +83,6 @@
83 83 </dependency>
84 84  
85 85 <!-- 工具类相关 -->
86   - <dependency>
87   - <groupId>com.github.ulisesbocchio</groupId>
88   - <artifactId>jasypt-spring-boot-starter</artifactId> <!-- 加解密 -->
89   - </dependency>
90 86  
91 87 <dependency>
92 88 <groupId>cn.iocoder.boot</groupId>
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java
1 1 package cn.iocoder.yudao.module.infra.service.db;
2 2  
3 3 import cn.hutool.core.util.ReflectUtil;
  4 +import cn.hutool.crypto.symmetric.AES;
4 5 import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler;
5 6 import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
6 7 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
... ... @@ -9,7 +10,6 @@ import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpda
9 10 import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
10 11 import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper;
11 12 import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
12   -import org.jasypt.encryption.StringEncryptor;
13 13 import org.junit.jupiter.api.BeforeEach;
14 14 import org.junit.jupiter.api.Test;
15 15 import org.mockito.MockedStatic;
... ... @@ -24,7 +24,8 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic
24 24 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
25 25 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
26 26 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
27   -import static org.junit.jupiter.api.Assertions.*;
  27 +import static org.junit.jupiter.api.Assertions.assertNotNull;
  28 +import static org.junit.jupiter.api.Assertions.assertNull;
28 29 import static org.mockito.ArgumentMatchers.anyString;
29 30 import static org.mockito.ArgumentMatchers.eq;
30 31 import static org.mockito.Mockito.mockStatic;
... ... @@ -45,7 +46,7 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
45 46 private DataSourceConfigMapper dataSourceConfigMapper;
46 47  
47 48 @MockBean
48   - private StringEncryptor stringEncryptor;
  49 + private AES aes;
49 50  
50 51 @MockBean
51 52 private DynamicDataSourceProperties dynamicDataSourceProperties;
... ... @@ -53,9 +54,9 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
53 54 @BeforeEach
54 55 public void setUp() {
55 56 // mock 一个空实现的 StringEncryptor,避免 EncryptTypeHandler 报错
56   - ReflectUtil.setFieldValue(EncryptTypeHandler.class, "encryptor", stringEncryptor);
57   - when(stringEncryptor.encrypt(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
58   - when(stringEncryptor.decrypt(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
  57 + ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
  58 + when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
  59 + when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
59 60 }
60 61  
61 62 @Test
... ...
yudao-server/src/main/resources/application-dev.yaml
... ... @@ -63,10 +63,6 @@ spring:
63 63 database: 1 # 数据库索引
64 64 # password: 123456 # 密码,建议生产环境开启
65 65  
66   -jasypt:
67   - encryptor:
68   - password: yuanma # 加解密的秘钥
69   -
70 66 --- #################### 定时任务相关配置 ####################
71 67  
72 68 # Quartz 配置项,对应 QuartzProperties 配置类
... ...
yudao-server/src/main/resources/application-local.yaml
... ... @@ -45,37 +45,33 @@ spring:
45 45 datasource:
46 46 master:
47 47 name: ruoyi-vue-pro
48   - url: jdbc:mysql://139.9.196.247:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
  48 + url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
49 49 # url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
50 50 # url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
51 51 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
52 52 # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例
53 53 username: root
54   - password: ${RUOYI_VUE_PRO}
  54 + password: 123456
55 55 # username: sa
56 56 # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
57 57 slave: # 模拟从库,可根据自己需要修改
58 58 name: ruoyi-vue-pro
59   - url: jdbc:mysql://139.9.196.247:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
  59 + url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
60 60 # url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
61 61 # url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
62 62 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
63 63 # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例
64 64 username: root
65   - password: ${RUOYI_VUE_PRO}
  65 + password: 123456
66 66 # username: sa
67 67 # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
68 68  
69 69 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
70 70 redis:
71   - host: 139.9.196.247 # 地址
  71 + host: 127.0.0.1 # 地址
72 72 port: 6379 # 端口
73 73 database: 0 # 数据库索引
74   - password: 123456 # 密码,建议生产环境开启
75   -
76   -jasypt:
77   - encryptor:
78   - password: yuanma # 加解密的秘钥
  74 +# password: 123456 # 密码,建议生产环境开启
79 75  
80 76 --- #################### 定时任务相关配置 ####################
81 77  
... ...
yudao-server/src/main/resources/application.yaml
... ... @@ -56,6 +56,8 @@ mybatis-plus:
56 56 logic-delete-value: 1 # 逻辑已删除值(默认为 1)
57 57 logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
58 58 type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject
  59 + encryptor:
  60 + password: XDV71a+xqStEA3WH # 加解密的秘钥,可使用 https://www.imaegoo.com/2020/aes-key-generator/ 网站生成
59 61  
60 62 --- #################### 验证码相关配置 ####################
61 63  
... ...