feat(security/crypto): 新增支持密码编码器加密

This commit is contained in:
2025-07-22 22:46:42 +08:00
parent 58f9687c58
commit 38b6428662
16 changed files with 96 additions and 8 deletions

View File

@@ -16,6 +16,13 @@
<description>ContiNew Starter 安全模块 - 加密</description> <description>ContiNew Starter 安全模块 - 加密</description>
<dependencies> <dependencies>
<!-- 安全模块 - 密码编码器 -->
<dependency>
<groupId>top.continew.starter</groupId>
<artifactId>continew-starter-security-password</artifactId>
<optional>true</optional>
</dependency>
<!-- Hutool 加密解密模块(封装 JDK 中加密解密算法) --> <!-- Hutool 加密解密模块(封装 JDK 中加密解密算法) -->
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>

View File

@@ -24,9 +24,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.security.crypto.core.MyBatisDecryptInterceptor; import top.continew.starter.core.util.GeneralPropertySourceFactory;
import top.continew.starter.security.crypto.core.MyBatisEncryptInterceptor; import top.continew.starter.security.crypto.mybatis.MyBatisDecryptInterceptor;
import top.continew.starter.security.crypto.mybatis.MyBatisEncryptInterceptor;
import top.continew.starter.security.crypto.util.EncryptHelper; import top.continew.starter.security.crypto.util.EncryptHelper;
/** /**
@@ -39,6 +41,7 @@ import top.continew.starter.security.crypto.util.EncryptHelper;
@AutoConfiguration @AutoConfiguration
@EnableConfigurationProperties(CryptoProperties.class) @EnableConfigurationProperties(CryptoProperties.class)
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_CRYPTO, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_CRYPTO, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
@PropertySource(value = "classpath:default-crypto.yml", factory = GeneralPropertySourceFactory.class)
public class CryptoAutoConfiguration { public class CryptoAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(CryptoAutoConfiguration.class); private static final Logger log = LoggerFactory.getLogger(CryptoAutoConfiguration.class);

View File

@@ -14,8 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
package top.continew.starter.security.crypto.encryptor; package top.continew.starter.security.crypto.autoconfigure;
import top.continew.starter.security.crypto.encryptor.IEncryptor;
import top.continew.starter.security.crypto.enums.Algorithm; import top.continew.starter.security.crypto.enums.Algorithm;
import java.util.Objects; import java.util.Objects;

View File

@@ -16,6 +16,8 @@
package top.continew.starter.security.crypto.encryptor; package top.continew.starter.security.crypto.encryptor;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
/** /**
* 加密器基类 * 加密器基类
* *

View File

@@ -20,6 +20,7 @@ import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto; import cn.hutool.crypto.symmetric.SymmetricCrypto;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;

View File

@@ -17,6 +17,7 @@
package top.continew.starter.security.crypto.encryptor; package top.continew.starter.security.crypto.encryptor;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
/** /**
* AESAdvanced Encryption Standard 加/解密处理器 * AESAdvanced Encryption Standard 加/解密处理器

View File

@@ -17,6 +17,7 @@
package top.continew.starter.security.crypto.encryptor; package top.continew.starter.security.crypto.encryptor;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
/** /**
* DESData Encryption Standard 加/解密处理器 * DESData Encryption Standard 加/解密处理器

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.security.crypto.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.password.autoconfigure.PasswordEncoderProperties;
/**
* 密码编码器加/解密处理器
*
* <p>
* 使用前必须注入 {@link PasswordEncoder},此加密方式不可逆,适合于密码场景
* </p>
*
* @see PasswordEncoder
* @see PasswordEncoderProperties
*
* @author Charles7c
* @since 2.13.3
*/
public class PasswordEncoderEncryptor extends AbstractEncryptor {
private final PasswordEncoder passwordEncoder = SpringUtil.getBean(PasswordEncoder.class);
private final PasswordEncoderProperties properties = SpringUtil.getBean(PasswordEncoderProperties.class);
public PasswordEncoderEncryptor(CryptoContext context) {
super(context);
}
@Override
public String encrypt(String plaintext) {
// 如果已经是加密格式,直接返回
if (properties.getAlgorithm().getPattern().matcher(plaintext).matches()) {
return plaintext;
}
return passwordEncoder.encode(plaintext);
}
@Override
public String decrypt(String ciphertext) {
return ciphertext;
}
}

View File

@@ -17,6 +17,7 @@
package top.continew.starter.security.crypto.encryptor; package top.continew.starter.security.crypto.encryptor;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
/** /**
* PBEWithMD5AndDESPassword Based Encryption With MD5 And DES 加/解密处理器 * PBEWithMD5AndDESPassword Based Encryption With MD5 And DES 加/解密处理器

View File

@@ -19,6 +19,7 @@ package top.continew.starter.security.crypto.encryptor;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.KeyType;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
/** /**
* RSA 加/解密处理器 * RSA 加/解密处理器

View File

@@ -55,7 +55,12 @@ public enum Algorithm {
/** /**
* Base64 * Base64
*/ */
BASE64(Base64Encryptor.class),; BASE64(Base64Encryptor.class),
/**
* 密码编码器支持算法BCrypt、SCRYPT、PBKDF2、ARGON2
*/
PASSWORD_ENCODER(PasswordEncoderEncryptor.class);
/** /**
* 加密/解密处理器 * 加密/解密处理器

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package top.continew.starter.security.crypto.core; package top.continew.starter.security.crypto.mybatis;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package top.continew.starter.security.crypto.core; package top.continew.starter.security.crypto.mybatis;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package top.continew.starter.security.crypto.core; package top.continew.starter.security.crypto.mybatis;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.ClassUtil;

View File

@@ -22,7 +22,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import top.continew.starter.security.crypto.annotation.FieldEncrypt; import top.continew.starter.security.crypto.annotation.FieldEncrypt;
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties; import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
import top.continew.starter.security.crypto.encryptor.CryptoContext; import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
import top.continew.starter.security.crypto.encryptor.IEncryptor; import top.continew.starter.security.crypto.encryptor.IEncryptor;
import top.continew.starter.security.crypto.enums.Algorithm; import top.continew.starter.security.crypto.enums.Algorithm;

View File

@@ -0,0 +1,6 @@
--- ### 安全配置:字段加/解密配置
continew-starter.security:
crypto:
enabled: true
# 默认算法,即 @FieldEncrypt 默认采用的算法默认AES 对称加密算法)
algorithm: AES