init
This commit is contained in:
32
ruoyi-common/ruoyi-common-jasypt/pom.xml
Normal file
32
ruoyi-common/ruoyi-common-jasypt/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-jasypt</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,38 @@
|
||||
package org.dromara.common.jasypt.config;
|
||||
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
||||
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Jasypt 加密配置类
|
||||
*
|
||||
* @author SkySource
|
||||
* @Date: 2025/7/2 9:52
|
||||
*/
|
||||
@Configuration
|
||||
public class JasyptConfig {
|
||||
/**
|
||||
* 自定义 StringEncryptor,覆盖默认的 StringEncryptor
|
||||
* bean 名称是必需的,从 1.5 版开始按名称检测自定义字符串加密程序,默认 bean 名称为:jasyptStringEncryptor
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean("jasyptStringEncryptor")
|
||||
public StringEncryptor jasyptStringEncryptor() {
|
||||
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
|
||||
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
|
||||
config.setPassword(System.getenv("PASSWD"));
|
||||
config.setAlgorithm("PBEWithMD5AndDES");
|
||||
config.setKeyObtentionIterations("1000");
|
||||
config.setPoolSize("1");
|
||||
config.setProviderName("SunJCE");
|
||||
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
|
||||
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
|
||||
config.setStringOutputType("base64");
|
||||
encryptor.setConfig(config);
|
||||
return encryptor;
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package org.dromara.common.jasypt.utils;
|
||||
|
||||
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
||||
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
|
||||
|
||||
public class JasyptUtils {
|
||||
/**
|
||||
* {@link StringEncryptor} 加解密。
|
||||
* 同一个密钥(secretKey)对同一个内容执行加密,生成的密文都是不一样的,但是根据根据这些密文解密成明文都是可以.
|
||||
* 1、Jasypt 默认使用 {@link StringEncryptor} 来解密全局配置文件中的属性,所以提供密文时,也需要提供 {@link StringEncryptor} 加密的密文
|
||||
* 2、{@link StringEncryptor} 接口有很多的实现类,比如常用的 {@link PooledPBEStringEncryptor}
|
||||
* 3、setConfig(final PBEConfig config):为对象设置 {@link PBEConfig} 配置对象
|
||||
* 4、encrypt(final String message):加密内容
|
||||
* 5、decrypt(final String encryptedMessage):解密内容
|
||||
*
|
||||
* @param secretKey :密钥。加/解密必须使用同一个密钥
|
||||
* @param message :加/解密的内容
|
||||
* @param isEncrypt :true 表示加密、false 表示解密
|
||||
* @return
|
||||
*/
|
||||
public static String stringEncryptor(String secretKey, String message, boolean isEncrypt) {
|
||||
PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
|
||||
pooledPBEStringEncryptor.setConfig(getSimpleStringPBEConfig(secretKey));
|
||||
String result = isEncrypt ? pooledPBEStringEncryptor.encrypt(message) : pooledPBEStringEncryptor.decrypt(message);
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 设置 {@link PBEConfig} 配置对象,SimpleStringPBEConfig 是它的实现类
|
||||
* 1、所有的配置项建议与全局配置文件中的配置项保持一致,特别是 password、algorithm 等等选项,如果不一致,则应用启动时解密失败而报错.
|
||||
* 2、setPassword(final String password):设置加密密钥,必须与全局配置文件中配置的保存一致,否则应用启动时会解密失败而报错.
|
||||
* 3、setPoolSize(final String poolSize):设置要创建的加密程序池的大小.
|
||||
* 4、setAlgorithm(final String algorithm): 设置加密算法的值, 此算法必须由 JCE 提供程序支持
|
||||
* 5、setKeyObtentionIterations: 设置应用于获取加密密钥的哈希迭代次数。
|
||||
* 6、setProviderName(final String providerName):设置要请求加密算法的安全提供程序的名称
|
||||
* 7、setSaltGeneratorClassName:设置 Sal 发生器
|
||||
* 8、setIvGeneratorClassName:设置 IV 发生器
|
||||
* 9、setStringOutputType:设置字符串输出的编码形式。可用的编码类型有 base64、hexadecimal
|
||||
*
|
||||
* @param secretKey
|
||||
* @return
|
||||
*/
|
||||
private static SimpleStringPBEConfig getSimpleStringPBEConfig(String secretKey) {
|
||||
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
|
||||
config.setPassword(secretKey);
|
||||
config.setPoolSize("1");
|
||||
config.setAlgorithm("PBEWithMD5AndDES");
|
||||
config.setKeyObtentionIterations("1000");
|
||||
config.setProviderName("SunJCE");
|
||||
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
|
||||
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
|
||||
config.setStringOutputType("base64");
|
||||
return config;
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.dromara.common.jasypt.config.JasyptConfig
|
Reference in New Issue
Block a user