Skip to content

Commit 38b6428

Browse files
committed
feat(security/crypto): 新增支持密码编码器加密
1 parent 58f9687 commit 38b6428

16 files changed

Lines changed: 96 additions & 8 deletions

‎continew-starter-security/continew-starter-security-crypto/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
<description>ContiNew Starter 安全模块 - 加密</description>
1717

1818
<dependencies>
19+
<!-- 安全模块 - 密码编码器 -->
20+
<dependency>
21+
<groupId>top.continew.starter</groupId>
22+
<artifactId>continew-starter-security-password</artifactId>
23+
<optional>true</optional>
24+
</dependency>
25+
1926
<!-- Hutool 加密解密模块(封装 JDK 中加密解密算法) -->
2027
<dependency>
2128
<groupId>cn.hutool</groupId>

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoAutoConfiguration.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2626
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.PropertySource;
2728
import top.continew.starter.core.constant.PropertiesConstants;
28-
import top.continew.starter.security.crypto.core.MyBatisDecryptInterceptor;
29-
import top.continew.starter.security.crypto.core.MyBatisEncryptInterceptor;
29+
import top.continew.starter.core.util.GeneralPropertySourceFactory;
30+
import top.continew.starter.security.crypto.mybatis.MyBatisDecryptInterceptor;
31+
import top.continew.starter.security.crypto.mybatis.MyBatisEncryptInterceptor;
3032
import top.continew.starter.security.crypto.util.EncryptHelper;
3133

3234
/**
@@ -39,6 +41,7 @@
3941
@AutoConfiguration
4042
@EnableConfigurationProperties(CryptoProperties.class)
4143
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_CRYPTO, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
44+
@PropertySource(value = "classpath:default-crypto.yml", factory = GeneralPropertySourceFactory.class)
4245
public class CryptoAutoConfiguration {
4346

4447
private static final Logger log = LoggerFactory.getLogger(CryptoAutoConfiguration.class);

continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/CryptoContext.java renamed to continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.crypto.encryptor;
17+
package top.continew.starter.security.crypto.autoconfigure;
1818

19+
import top.continew.starter.security.crypto.encryptor.IEncryptor;
1920
import top.continew.starter.security.crypto.enums.Algorithm;
2021

2122
import java.util.Objects;

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/AbstractEncryptor.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package top.continew.starter.security.crypto.encryptor;
1818

19+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
20+
1921
/**
2022
* 加密器基类
2123
*

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/AbstractSymmetricCryptoEncryptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
2121
import cn.hutool.crypto.symmetric.SymmetricCrypto;
2222
import top.continew.starter.core.constant.StringConstants;
23+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
2324

2425
import java.nio.charset.StandardCharsets;
2526
import java.util.Map;

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/AesEncryptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.security.crypto.encryptor;
1818

1919
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
20+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
2021

2122
/**
2223
* AES(Advanced Encryption Standard) 加/解密处理器

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/DesEncryptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.security.crypto.encryptor;
1818

1919
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
20+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
2021

2122
/**
2223
* DES(Data Encryption Standard) 加/解密处理器
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.security.crypto.encryptor;
18+
19+
import cn.hutool.extra.spring.SpringUtil;
20+
import org.springframework.security.crypto.password.PasswordEncoder;
21+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
22+
import top.continew.starter.security.password.autoconfigure.PasswordEncoderProperties;
23+
24+
/**
25+
* 密码编码器加/解密处理器
26+
*
27+
* <p>
28+
* 使用前必须注入 {@link PasswordEncoder},此加密方式不可逆,适合于密码场景
29+
* </p>
30+
*
31+
* @see PasswordEncoder
32+
* @see PasswordEncoderProperties
33+
*
34+
* @author Charles7c
35+
* @since 2.13.3
36+
*/
37+
public class PasswordEncoderEncryptor extends AbstractEncryptor {
38+
39+
private final PasswordEncoder passwordEncoder = SpringUtil.getBean(PasswordEncoder.class);
40+
private final PasswordEncoderProperties properties = SpringUtil.getBean(PasswordEncoderProperties.class);
41+
42+
public PasswordEncoderEncryptor(CryptoContext context) {
43+
super(context);
44+
}
45+
46+
@Override
47+
public String encrypt(String plaintext) {
48+
// 如果已经是加密格式,直接返回
49+
if (properties.getAlgorithm().getPattern().matcher(plaintext).matches()) {
50+
return plaintext;
51+
}
52+
return passwordEncoder.encode(plaintext);
53+
}
54+
55+
@Override
56+
public String decrypt(String ciphertext) {
57+
return ciphertext;
58+
}
59+
}

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/PbeWithMd5AndDesEncryptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.security.crypto.encryptor;
1818

1919
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
20+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
2021

2122
/**
2223
* PBEWithMD5AndDES(Password Based Encryption With MD5 And DES) 加/解密处理器

‎continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/RsaEncryptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cn.hutool.core.codec.Base64;
2020
import cn.hutool.crypto.SecureUtil;
2121
import cn.hutool.crypto.asymmetric.KeyType;
22+
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
2223

2324
/**
2425
* RSA 加/解密处理器

0 commit comments

Comments
 (0)