feat(security/crypto): 新增密码编码器配置(由原 security/password 模块融合)

This commit is contained in:
2025-07-25 21:43:13 +08:00
parent ecabda6aec
commit 0ba365dabc
16 changed files with 67 additions and 153 deletions

View File

@@ -167,9 +167,9 @@ continew-starter
│ └─ continew-starter-data-mfMyBatis Flex
├─ continew-starter-security安全模块
│ ├─ continew-starter-security-crypto加密字段加解密
│ ├─ continew-starter-security-xssXSS 过滤)
│ ├─ continew-starter-security-mask脱敏JSON 数据脱敏)
─ continew-starter-security-password密码编码器
─ continew-starter-security-xssXSS 过滤
│ └─ continew-starter-security-sensitivewords敏感词
├─ continew-starter-ratelimiter限流模块
├─ continew-starter-idempotent幂等模块
├─ continew-starter-trace链路追踪模块

View File

@@ -104,12 +104,6 @@
<version>${revision}</version>
</dependency>
<!-- 安全模块 - 密码编码器 -->
<dependency>
<groupId>top.continew.starter</groupId>
<artifactId>continew-starter-security-password</artifactId>
<version>${revision}</version>
</dependency>
<!-- 安全模块 - 加密 -->
<dependency>
<groupId>top.continew.starter</groupId>

View File

@@ -54,11 +54,6 @@ public class PropertiesConstants {
*/
public static final String SECURITY = CONTINEW_STARTER + StringConstants.DOT + "security";
/**
* 安全-密码编解码配置
*/
public static final String SECURITY_PASSWORD = SECURITY + StringConstants.DOT + "password";
/**
* 安全-加/解密配置
*/

View File

@@ -16,19 +16,18 @@
<description>ContiNew Starter 安全模块 - 加密</description>
<dependencies>
<!-- 安全模块 - 密码编码器 -->
<dependency>
<groupId>top.continew.starter</groupId>
<artifactId>continew-starter-security-password</artifactId>
<optional>true</optional>
</dependency>
<!-- Hutool 加密解密模块(封装 JDK 中加密解密算法) -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
</dependency>
<!-- Spring Security 附带的一个密码加密库 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
<!-- MyBatis PlusMyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
<dependency>
<groupId>com.baomidou</groupId>

View File

@@ -25,11 +25,20 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.security.crypto.enums.PasswordEncoderAlgorithm;
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.PasswordEncoderUtil;
import java.util.HashMap;
import java.util.Map;
/**
* 加/解密自动配置
@@ -69,6 +78,31 @@ public class CryptoAutoConfiguration {
return new MyBatisDecryptInterceptor();
}
/**
* 密码编码器配置
*
* @see DelegatingPasswordEncoder
* @see PasswordEncoderFactories
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_CRYPTO + ".password-encoder", name = PropertiesConstants.ENABLED, havingValue = "true")
public PasswordEncoder passwordEncoder() {
PasswordEncoderProperties passwordEncoderProperties = properties.getPasswordEncoder();
Map<String, PasswordEncoder> encoders = new HashMap<>();
encoders.put(PasswordEncoderAlgorithm.BCRYPT.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.BCRYPT));
encoders.put(PasswordEncoderAlgorithm.SCRYPT.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.SCRYPT));
encoders.put(PasswordEncoderAlgorithm.PBKDF2.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.PBKDF2));
encoders.put(PasswordEncoderAlgorithm.ARGON2.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.ARGON2));
PasswordEncoderAlgorithm algorithm = passwordEncoderProperties.getAlgorithm();
CheckUtils.throwIf(PasswordEncoderUtil.getEncoder(algorithm) == null, "不支持的加密算法: {}", algorithm);
return new DelegatingPasswordEncoder(algorithm.name().toLowerCase(), encoders);
}
@PostConstruct
public void postConstruct() {
EncryptHelper.init(properties);

View File

@@ -17,6 +17,7 @@
package top.continew.starter.security.crypto.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;
@@ -55,6 +56,12 @@ public class CryptoProperties {
*/
private String privateKey;
/**
* 密码编码器配置
*/
@NestedConfigurationProperty
private PasswordEncoderProperties passwordEncoder;
public boolean isEnabled() {
return enabled;
}
@@ -94,4 +101,12 @@ public class CryptoProperties {
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public PasswordEncoderProperties getPasswordEncoder() {
return passwordEncoder;
}
public void setPasswordEncoder(PasswordEncoderProperties passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
}

View File

@@ -14,11 +14,9 @@
* limitations under the License.
*/
package top.continew.starter.security.password.autoconfigure;
package top.continew.starter.security.crypto.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.security.password.enums.PasswordEncoderAlgorithm;
import top.continew.starter.security.crypto.enums.PasswordEncoderAlgorithm;
/**
* 密码编解码配置属性
@@ -26,7 +24,6 @@ import top.continew.starter.security.password.enums.PasswordEncoderAlgorithm;
* @author Jasmine
* @since 1.3.0
*/
@ConfigurationProperties(PropertiesConstants.SECURITY_PASSWORD)
public class PasswordEncoderProperties {
/**

View File

@@ -19,7 +19,7 @@ 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;
import top.continew.starter.security.crypto.autoconfigure.PasswordEncoderProperties;
/**
* 密码编码器加/解密处理器

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.security.password.enums;
package top.continew.starter.security.crypto.enums;
import java.util.regex.Pattern;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.security.password.exception;
package top.continew.starter.security.crypto.exception;
import top.continew.starter.core.exception.BaseException;

View File

@@ -14,15 +14,15 @@
* limitations under the License.
*/
package top.continew.starter.security.password.util;
package top.continew.starter.security.crypto.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.password.enums.PasswordEncoderAlgorithm;
import top.continew.starter.security.password.exception.PasswordEncodeException;
import top.continew.starter.security.crypto.enums.PasswordEncoderAlgorithm;
import top.continew.starter.security.crypto.exception.PasswordEncodeException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

View File

@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.continew.starter</groupId>
<artifactId>continew-starter-security</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-security-password</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 安全模块 - 密码编码器</description>
<dependencies>
<!-- Spring Security 附带的一个密码加密库 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -1,87 +0,0 @@
/*
* 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.password.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.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.security.password.enums.PasswordEncoderAlgorithm;
import top.continew.starter.security.password.util.PasswordEncoderUtil;
import java.util.HashMap;
import java.util.Map;
/**
* 密码编码器自动配置
*
* <p>
* 密码配置类,默认编解码器使用的是 BCryptPasswordEncoder <br />
* 编码后的密码是遵循一定规则的 {idForEncode}encodePassword前缀 {} 包含了编码的方式再拼接上该方式编码后的密码串。<br />
* 可以添加自定义的编解码,也可以修改默认的编解码器,只需修改默认的 encodingId。<br />
* 优点:如果有一天我们对密码编码规则进行替换或者轮转,现有的用户不会受到影响,只要修改 DelegatingPasswordEncoder 的 idForEncode 即可。
* </p>
*
* @author Jasmine
* @author Charles7c
* @since 1.3.0
*/
@AutoConfiguration
@EnableConfigurationProperties(PasswordEncoderProperties.class)
@PropertySource(value = "classpath:default-password.yml", factory = GeneralPropertySourceFactory.class)
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_PASSWORD, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
public class PasswordEncoderAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(PasswordEncoderAutoConfiguration.class);
/**
* 密码编码器
*
* @see DelegatingPasswordEncoder
* @see PasswordEncoderFactories
*/
@Bean
public PasswordEncoder passwordEncoder(PasswordEncoderProperties properties) {
Map<String, PasswordEncoder> encoders = new HashMap<>();
encoders.put(PasswordEncoderAlgorithm.BCRYPT.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.BCRYPT));
encoders.put(PasswordEncoderAlgorithm.SCRYPT.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.SCRYPT));
encoders.put(PasswordEncoderAlgorithm.PBKDF2.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.PBKDF2));
encoders.put(PasswordEncoderAlgorithm.ARGON2.name().toLowerCase(), PasswordEncoderUtil
.getEncoder(PasswordEncoderAlgorithm.ARGON2));
PasswordEncoderAlgorithm algorithm = properties.getAlgorithm();
CheckUtils.throwIf(PasswordEncoderUtil.getEncoder(algorithm) == null, "不支持的加密算法: {}", algorithm);
return new DelegatingPasswordEncoder(algorithm.name().toLowerCase(), encoders);
}
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Security-PasswordEncoder' completed initialization.");
}
}

View File

@@ -1 +0,0 @@
top.continew.starter.security.password.autoconfigure.PasswordEncoderAutoConfiguration

View File

@@ -1,6 +0,0 @@
--- ### 安全配置:密码编码器配置
continew-starter.security:
password:
enabled: true
# 默认启用的编码器算法默认BCrypt 加密算法)
algorithm: BCRYPT

View File

@@ -16,11 +16,10 @@
<description>ContiNew Starter 安全模块</description>
<modules>
<module>continew-starter-security-password</module>
<module>continew-starter-security-mask</module>
<module>continew-starter-security-crypto</module>
<module>continew-starter-security-sensitivewords</module>
<module>continew-starter-security-mask</module>
<module>continew-starter-security-xss</module>
<module>continew-starter-security-sensitivewords</module>
</modules>
<dependencies>