* 美国国家标准与技术研究院(NIST)采纳的对称加密算法标准,提供128位、192位和256位三种密钥长度,以高效和安全性著称。 *
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/Base64Encryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/Base64Encryptor.java similarity index 93% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/Base64Encryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/Base64Encryptor.java index 8e08dcfa..56b4fd8e 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/Base64Encryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/Base64Encryptor.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; import cn.hutool.core.codec.Base64; /** - * Base64 加/解密处理器 + * Base64 加密器 ** 一种用于编码二进制数据到文本格式的算法,常用于邮件附件、网页传输等场合,但它不是一种加密算法,只提供数据的编码和解码,不保证数据的安全性。 *
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/DesEncryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/DesEncryptor.java similarity index 86% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/DesEncryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/DesEncryptor.java index 49d1b132..24391a54 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/DesEncryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/DesEncryptor.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; import cn.hutool.crypto.symmetric.SymmetricAlgorithm; -import top.continew.starter.security.crypto.autoconfigure.CryptoContext; +import top.continew.starter.encrypt.context.CryptoContext; /** - * DES(Data Encryption Standard) 加/解密处理器 + * DES(Data Encryption Standard) 加密器 ** 一种对称加密算法,使用相同的密钥进行加密和解密。DES 使用 56 位密钥(实际上有 64 位,但有 8 位用于奇偶校验)和一系列置换和替换操作来加密数据。 *
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/IEncryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/IEncryptor.java similarity index 93% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/IEncryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/IEncryptor.java index 1ff83cf6..c49412f9 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/IEncryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/IEncryptor.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; /** - * 加/解密接口 + * 加密器接口 * * @author Charles7c * @author lishuyan diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PasswordEncoderEncryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PasswordEncoderEncryptor.java similarity index 67% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PasswordEncoderEncryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PasswordEncoderEncryptor.java index 2d11b7b5..809abe6a 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PasswordEncoderEncryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PasswordEncoderEncryptor.java @@ -14,16 +14,16 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; import cn.hutool.extra.spring.SpringUtil; import org.springframework.security.crypto.password.PasswordEncoder; -import top.continew.starter.security.crypto.autoconfigure.CryptoContext; -import top.continew.starter.security.crypto.autoconfigure.CryptoProperties; -import top.continew.starter.security.crypto.autoconfigure.PasswordEncoderProperties; +import top.continew.starter.core.util.SpringUtils; +import top.continew.starter.encrypt.autoconfigure.PasswordEncoderProperties; +import top.continew.starter.encrypt.context.CryptoContext; /** - * 密码编码器加/解密处理器 + * 密码编码器加密器 * ** 使用前必须注入 {@link PasswordEncoder},此加密方式不可逆,适合于密码场景 @@ -37,8 +37,7 @@ import top.continew.starter.security.crypto.autoconfigure.PasswordEncoderPropert */ public class PasswordEncoderEncryptor extends AbstractEncryptor { - private final PasswordEncoder passwordEncoder = SpringUtil.getBean(PasswordEncoder.class); - private final CryptoProperties properties = SpringUtil.getBean(CryptoProperties.class); + private final PasswordEncoderProperties properties = SpringUtils.getBean(PasswordEncoderProperties.class, true); public PasswordEncoderEncryptor(CryptoContext context) { super(context); @@ -47,10 +46,10 @@ public class PasswordEncoderEncryptor extends AbstractEncryptor { @Override public String encrypt(String plaintext) { // 如果已经是加密格式,直接返回 - if (properties.getPasswordEncoder().getAlgorithm().getPattern().matcher(plaintext).matches()) { + if (properties == null || properties.getAlgorithm().getPattern().matcher(plaintext).matches()) { return plaintext; } - return passwordEncoder.encode(plaintext); + return SpringUtil.getBean(PasswordEncoder.class).encode(plaintext); } @Override diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PbeWithMd5AndDesEncryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PbeWithMd5AndDesEncryptor.java similarity index 88% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PbeWithMd5AndDesEncryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PbeWithMd5AndDesEncryptor.java index c101577b..0e59b552 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PbeWithMd5AndDesEncryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/PbeWithMd5AndDesEncryptor.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; import cn.hutool.crypto.symmetric.SymmetricAlgorithm; -import top.continew.starter.security.crypto.autoconfigure.CryptoContext; +import top.continew.starter.encrypt.context.CryptoContext; /** - * PBEWithMD5AndDES(Password Based Encryption With MD5 And DES) 加/解密处理器 + * PBEWithMD5AndDES(Password Based Encryption With MD5 And DES) 加密器 *
* 混合加密算法,结合了 MD5 散列算法和 DES(Data Encryption Standard)加密算法 *
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/RsaEncryptor.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/RsaEncryptor.java similarity index 91% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/RsaEncryptor.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/RsaEncryptor.java index efa9f73b..ec24bf43 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/RsaEncryptor.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/encryptor/RsaEncryptor.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.encryptor; +package top.continew.starter.encrypt.encryptor; import cn.hutool.core.codec.Base64; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.asymmetric.KeyType; -import top.continew.starter.security.crypto.autoconfigure.CryptoContext; +import top.continew.starter.encrypt.context.CryptoContext; /** - * RSA 加/解密处理器 + * RSA 加密器 ** 非对称加密算法,由罗纳德·李维斯特(Ron Rivest)、阿迪·沙米尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)于1977年提出,安全性基于大数因子分解问题的困难性。 *
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/Algorithm.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/Algorithm.java similarity index 91% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/Algorithm.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/Algorithm.java index e19c4c37..5cfccb44 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/Algorithm.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/Algorithm.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.enums; +package top.continew.starter.encrypt.enums; -import top.continew.starter.security.crypto.encryptor.*; +import top.continew.starter.encrypt.encryptor.*; /** - * 加密/解密算法枚举 + * 加密算法枚举 * * @author Charles7c * @author lishuyan diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/PasswordEncoderAlgorithm.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/PasswordEncoderAlgorithm.java similarity index 96% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/PasswordEncoderAlgorithm.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/PasswordEncoderAlgorithm.java index 1a156177..cd9bcdd1 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/enums/PasswordEncoderAlgorithm.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/enums/PasswordEncoderAlgorithm.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.enums; +package top.continew.starter.encrypt.enums; import java.util.regex.Pattern; diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/exception/PasswordEncodeException.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/exception/PasswordEncodeException.java similarity index 95% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/exception/PasswordEncodeException.java rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/exception/PasswordEncodeException.java index 71e1958a..4c7c5f29 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/exception/PasswordEncodeException.java +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/exception/PasswordEncodeException.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.exception; +package top.continew.starter.encrypt.exception; import top.continew.starter.core.exception.BaseException; diff --git a/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/EncryptUtils.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/EncryptUtils.java new file mode 100644 index 00000000..aca8bc07 --- /dev/null +++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/EncryptUtils.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.continew.starter.encrypt.util;
+
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.text.CharSequenceUtil;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.crypto.SecureUtil;
+import cn.hutool.crypto.asymmetric.KeyType;
+import cn.hutool.crypto.asymmetric.RSA;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 加密工具类
+ *
+ * @author Charles7c
+ * @since 2.14.0
+ */
+public class EncryptUtils {
+
+ /**
+ * Base64 编码
+ *
+ * @param data 待编码数据
+ * @return 编码后字符串
+ * @author lishuyan
+ */
+ public static String encodeByBase64(String data) {
+ return Base64.encode(data, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * Base64 解码
+ *
+ * @param data 待解码数据
+ * @return 解码后字符串
+ * @author lishuyan
+ */
+ public static String decodeByBase64(String data) {
+ return Base64.decodeStr(data, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * AES 加密
+ *
+ * @param data 待加密数据
+ * @param password 秘钥字符串
+ * @return 加密后字符串, 采用 Base64 编码
+ * @author lishuyan
+ */
+ public static String encryptByAes(String data, String password) {
+ if (CharSequenceUtil.isBlank(password)) {
+ throw new IllegalArgumentException("AES需要传入秘钥信息");
+ }
+ // AES算法的秘钥要求是16位、24位、32位
+ int[] array = {16, 24, 32};
+ if (!ArrayUtil.contains(array, password.length())) {
+ throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
+ }
+ return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * AES 解密
+ *
+ * @param data 待解密数据
+ * @param password 秘钥字符串
+ * @return 解密后字符串
+ * @author lishuyan
+ */
+ public static String decryptByAes(String data, String password) {
+ if (CharSequenceUtil.isBlank(password)) {
+ throw new IllegalArgumentException("AES需要传入秘钥信息");
+ }
+ // AES算法的秘钥要求是16位、24位、32位
+ int[] array = {16, 24, 32};
+ if (!ArrayUtil.contains(array, password.length())) {
+ throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
+ }
+ return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * RSA 公钥加密
+ *
+ * @param data 待加密数据
+ * @param publicKey 公钥
+ * @return 加密后字符串, 采用Base64编码
+ * @author lishuyan
+ */
+ public static String encryptByRsa(String data, String publicKey) {
+ if (CharSequenceUtil.isBlank(publicKey)) {
+ throw new IllegalArgumentException("RSA需要传入公钥进行加密");
+ }
+ RSA rsa = SecureUtil.rsa(null, publicKey);
+ return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
+ }
+
+ /**
+ * RSA 私钥解密
+ *
+ * @param data 待解密数据
+ * @param privateKey 私钥
+ * @return 解密后字符串
+ * @author lishuyan
+ */
+ public static String decryptByRsa(String data, String privateKey) {
+ if (CharSequenceUtil.isBlank(privateKey)) {
+ throw new IllegalArgumentException("RSA需要传入私钥进行解密");
+ }
+ RSA rsa = SecureUtil.rsa(privateKey, null);
+ return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
+ }
+
+ private EncryptUtils() {
+ }
+}
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/PasswordEncoderUtil.java b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/PasswordEncoderUtil.java
similarity index 95%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/PasswordEncoderUtil.java
rename to continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/PasswordEncoderUtil.java
index a2cc387f..04fe36d6 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/PasswordEncoderUtil.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/java/top/continew/starter/encrypt/util/PasswordEncoderUtil.java
@@ -14,15 +14,15 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.util;
+package top.continew.starter.encrypt.util;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
-import top.continew.starter.security.crypto.enums.PasswordEncoderAlgorithm;
-import top.continew.starter.security.crypto.exception.PasswordEncodeException;
+import top.continew.starter.encrypt.enums.PasswordEncoderAlgorithm;
+import top.continew.starter.encrypt.exception.PasswordEncodeException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
diff --git a/continew-starter-encrypt/continew-starter-encrypt-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 00000000..9e47c38a
--- /dev/null
+++ b/continew-starter-encrypt/continew-starter-encrypt-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+top.continew.starter.encrypt.autoconfigure.PasswordEncoderAutoConfiguration
\ No newline at end of file
diff --git a/continew-starter-encrypt/continew-starter-encrypt-field/pom.xml b/continew-starter-encrypt/continew-starter-encrypt-field/pom.xml
new file mode 100644
index 00000000..e8472d64
--- /dev/null
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/pom.xml
@@ -0,0 +1,31 @@
+
+
- * 优先级高于加密/解密算法 + * 优先级高于加密算法 *
*/ Class extends IEncryptor> encryptor() default IEncryptor.class; diff --git a/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptAutoConfiguration.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptAutoConfiguration.java new file mode 100644 index 00000000..3184240a --- /dev/null +++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptAutoConfiguration.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.continew.starter.encrypt.field.autoconfigure;
+
+import jakarta.annotation.PostConstruct;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import top.continew.starter.core.constant.PropertiesConstants;
+import top.continew.starter.encrypt.field.interceptor.MyBatisDecryptInterceptor;
+import top.continew.starter.encrypt.field.interceptor.MyBatisEncryptInterceptor;
+import top.continew.starter.encrypt.field.util.EncryptHelper;
+
+/**
+ * 字段加密自动配置
+ *
+ * @author Charles7c
+ * @author lishuyan
+ * @since 1.4.0
+ */
+@AutoConfiguration
+@EnableConfigurationProperties(FieldEncryptProperties.class)
+@ConditionalOnProperty(prefix = PropertiesConstants.ENCRYPT_FIELD, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
+public class FieldEncryptAutoConfiguration {
+
+ private static final Logger log = LoggerFactory.getLogger(FieldEncryptAutoConfiguration.class);
+ private final FieldEncryptProperties properties;
+
+ public FieldEncryptAutoConfiguration(FieldEncryptProperties properties) {
+ this.properties = properties;
+ }
+
+ /**
+ * MyBatis 加密拦截器配置
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public MyBatisEncryptInterceptor mybatisEncryptInterceptor() {
+ return new MyBatisEncryptInterceptor();
+ }
+
+ /**
+ * MyBatis 解密拦截器配置
+ */
+ @Bean
+ @ConditionalOnMissingBean(MyBatisDecryptInterceptor.class)
+ public MyBatisDecryptInterceptor mybatisDecryptInterceptor() {
+ return new MyBatisDecryptInterceptor();
+ }
+
+ @PostConstruct
+ public void postConstruct() {
+ EncryptHelper.init(properties);
+ log.debug("[ContiNew Starter] - Auto Configuration 'Encrypt-Field' completed initialization.");
+ }
+}
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoProperties.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptProperties.java
similarity index 84%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoProperties.java
rename to continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptProperties.java
index 78ce2363..fa50e070 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoProperties.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/autoconfigure/FieldEncryptProperties.java
@@ -14,27 +14,28 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.autoconfigure;
+package top.continew.starter.encrypt.field.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import top.continew.starter.core.constant.PropertiesConstants;
-import top.continew.starter.security.crypto.enums.Algorithm;
+import top.continew.starter.encrypt.autoconfigure.PasswordEncoderProperties;
+import top.continew.starter.encrypt.enums.Algorithm;
/**
- * 加/解密配置属性
+ * 字段加密配置属性
*
* @author Charles7c
* @author lishuyan
* @since 1.4.0
*/
-@ConfigurationProperties(PropertiesConstants.SECURITY_CRYPTO)
-public class CryptoProperties {
+@ConfigurationProperties(PropertiesConstants.ENCRYPT_FIELD)
+public class FieldEncryptProperties {
/**
* 是否启用
*/
- private boolean enabled = true;
+ private Boolean enabled;
/**
* 默认算法
@@ -62,11 +63,11 @@ public class CryptoProperties {
@NestedConfigurationProperty
private PasswordEncoderProperties passwordEncoder;
- public boolean isEnabled() {
+ public Boolean getEnabled() {
return enabled;
}
- public void setEnabled(boolean enabled) {
+ public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/AbstractMyBatisInterceptor.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/AbstractMyBatisInterceptor.java
similarity index 97%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/AbstractMyBatisInterceptor.java
rename to continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/AbstractMyBatisInterceptor.java
index b434adc3..e67127d4 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/AbstractMyBatisInterceptor.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/AbstractMyBatisInterceptor.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.mybatis;
+package top.continew.starter.encrypt.field.interceptor;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReflectUtil;
@@ -22,7 +22,7 @@ import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.mapping.MappedStatement;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BaseException;
-import top.continew.starter.security.crypto.annotation.FieldEncrypt;
+import top.continew.starter.encrypt.field.annotation.FieldEncrypt;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisDecryptInterceptor.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisDecryptInterceptor.java
similarity index 95%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisDecryptInterceptor.java
rename to continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisDecryptInterceptor.java
index 0f6f7621..fb78d99e 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisDecryptInterceptor.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisDecryptInterceptor.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.mybatis;
+package top.continew.starter.encrypt.field.interceptor;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
@@ -26,8 +26,8 @@ import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.type.SimpleTypeRegistry;
-import top.continew.starter.security.crypto.annotation.FieldEncrypt;
-import top.continew.starter.security.crypto.util.EncryptHelper;
+import top.continew.starter.encrypt.field.annotation.FieldEncrypt;
+import top.continew.starter.encrypt.field.util.EncryptHelper;
import java.lang.reflect.Field;
import java.sql.Statement;
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisEncryptInterceptor.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisEncryptInterceptor.java
similarity index 97%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisEncryptInterceptor.java
rename to continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisEncryptInterceptor.java
index 371bdba9..9257ba87 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/mybatis/MyBatisEncryptInterceptor.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/interceptor/MyBatisEncryptInterceptor.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.mybatis;
+package top.continew.starter.encrypt.field.interceptor;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ClassUtil;
@@ -29,8 +29,8 @@ import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import top.continew.starter.core.constant.StringConstants;
-import top.continew.starter.security.crypto.annotation.FieldEncrypt;
-import top.continew.starter.security.crypto.util.EncryptHelper;
+import top.continew.starter.encrypt.field.annotation.FieldEncrypt;
+import top.continew.starter.encrypt.field.util.EncryptHelper;
import java.lang.reflect.Field;
import java.util.Arrays;
diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/util/EncryptHelper.java
similarity index 63%
rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java
rename to continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/util/EncryptHelper.java
index d891726d..c86f85d8 100644
--- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/java/top/continew/starter/encrypt/field/util/EncryptHelper.java
@@ -14,25 +14,19 @@
* limitations under the License.
*/
-package top.continew.starter.security.crypto.util;
+package top.continew.starter.encrypt.field.util;
-import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.CharSequenceUtil;
-import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
-import cn.hutool.crypto.SecureUtil;
-import cn.hutool.crypto.asymmetric.KeyType;
-import cn.hutool.crypto.asymmetric.RSA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import top.continew.starter.security.crypto.annotation.FieldEncrypt;
-import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
-import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
-import top.continew.starter.security.crypto.encryptor.IEncryptor;
-import top.continew.starter.security.crypto.enums.Algorithm;
+import top.continew.starter.encrypt.context.CryptoContext;
+import top.continew.starter.encrypt.encryptor.IEncryptor;
+import top.continew.starter.encrypt.enums.Algorithm;
+import top.continew.starter.encrypt.field.annotation.FieldEncrypt;
+import top.continew.starter.encrypt.field.autoconfigure.FieldEncryptProperties;
import java.lang.reflect.Field;
-import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -49,7 +43,7 @@ public class EncryptHelper {
/**
* 默认加密配置
*/
- private static CryptoProperties defaultProperties;
+ private static FieldEncryptProperties defaultProperties;
/**
* 加密器缓存
@@ -64,7 +58,7 @@ public class EncryptHelper {
*
* @param properties 加密配置
*/
- public static void init(CryptoProperties properties) {
+ public static void init(FieldEncryptProperties properties) {
defaultProperties = properties;
}
@@ -105,7 +99,7 @@ public class EncryptHelper {
* @return 加密后的字符串
*/
public static String encrypt(String value, FieldEncrypt fieldEncrypt) {
- if (CharSequenceUtil.isBlank(value) || fieldEncrypt == null || !defaultProperties.isEnabled()) {
+ if (CharSequenceUtil.isBlank(value) || fieldEncrypt == null) {
return value;
}
String ciphertext = value;
@@ -126,7 +120,7 @@ public class EncryptHelper {
* @return 加密后的字符串
*/
public static String encrypt(String value) {
- if (CharSequenceUtil.isBlank(value) || !defaultProperties.isEnabled()) {
+ if (CharSequenceUtil.isBlank(value)) {
return value;
}
String ciphertext = value;
@@ -148,7 +142,7 @@ public class EncryptHelper {
* @return 解密后的字符串
*/
public static String decrypt(String value, FieldEncrypt fieldEncrypt) {
- if (CharSequenceUtil.isBlank(value) || fieldEncrypt == null || !defaultProperties.isEnabled()) {
+ if (CharSequenceUtil.isBlank(value) || fieldEncrypt == null) {
return value;
}
String plaintext = value;
@@ -169,7 +163,7 @@ public class EncryptHelper {
* @return 解密后的字符串
*/
public static String decrypt(String value) {
- if (CharSequenceUtil.isBlank(value) || !defaultProperties.isEnabled()) {
+ if (CharSequenceUtil.isBlank(value)) {
return value;
}
String plaintext = value;
@@ -223,98 +217,4 @@ public class EncryptHelper {
cryptoContext.setPublicKey(defaultProperties.getPublicKey());
return cryptoContext;
}
-
- /**
- * Base64编码
- *
- * @param data 待编码数据
- * @return 编码后字符串
- * @since 2.14.0
- */
- public static String encodeByBase64(String data) {
- return Base64.encode(data, StandardCharsets.UTF_8);
- }
-
- /**
- * Base64解码
- *
- * @param data 待解码数据
- * @return 解码后字符串
- * @since 2.14.0
- */
- public static String decodeByBase64(String data) {
- return Base64.decodeStr(data, StandardCharsets.UTF_8);
- }
-
- /**
- * AES加密
- *
- * @param data 待加密数据
- * @param password 秘钥字符串
- * @return 加密后字符串, 采用Base64编码
- * @since 2.14.0
- */
- public static String encryptByAes(String data, String password) {
- if (CharSequenceUtil.isBlank(password)) {
- throw new IllegalArgumentException("AES需要传入秘钥信息");
- }
- // AES算法的秘钥要求是16位、24位、32位
- int[] array = {16, 24, 32};
- if (!ArrayUtil.contains(array, password.length())) {
- throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
- }
- return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
- }
-
- /**
- * AES解密
- *
- * @param data 待解密数据
- * @param password 秘钥字符串
- * @return 解密后字符串
- * @since 2.14.0
- */
- public static String decryptByAes(String data, String password) {
- if (CharSequenceUtil.isBlank(password)) {
- throw new IllegalArgumentException("AES需要传入秘钥信息");
- }
- // AES算法的秘钥要求是16位、24位、32位
- int[] array = {16, 24, 32};
- if (!ArrayUtil.contains(array, password.length())) {
- throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
- }
- return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8);
- }
-
- /**
- * RSA公钥加密
- *
- * @param data 待加密数据
- * @param publicKey 公钥
- * @return 加密后字符串, 采用Base64编码
- * @since 2.14.0
- */
- public static String encryptByRsa(String data, String publicKey) {
- if (CharSequenceUtil.isBlank(publicKey)) {
- throw new IllegalArgumentException("RSA需要传入公钥进行加密");
- }
- RSA rsa = SecureUtil.rsa(null, publicKey);
- return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
- }
-
- /**
- * RSA私钥解密
- *
- * @param data 待解密数据
- * @param privateKey 私钥
- * @return 解密后字符串
- * @since 2.14.0
- */
- public static String decryptByRsa(String data, String privateKey) {
- if (CharSequenceUtil.isBlank(privateKey)) {
- throw new IllegalArgumentException("RSA需要传入私钥进行解密");
- }
- RSA rsa = SecureUtil.rsa(privateKey, null);
- return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
- }
}
diff --git a/continew-starter-encrypt/continew-starter-encrypt-field/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 00000000..8fff51d5
--- /dev/null
+++ b/continew-starter-encrypt/continew-starter-encrypt-field/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+top.continew.starter.encrypt.field.autoconfigure.FieldEncryptAutoConfiguration
\ No newline at end of file
diff --git a/continew-starter-encrypt/pom.xml b/continew-starter-encrypt/pom.xml
new file mode 100644
index 00000000..14c393eb
--- /dev/null
+++ b/continew-starter-encrypt/pom.xml
@@ -0,0 +1,23 @@
+
+