项目集成jasypt的方式
引入jasypt-spring-boot加密组件
通过jasypt-spring-boot这个开箱即用的加密组件来引入Jasypt这个强大的加密库
方式一:
在Springboot应用程序中,如果使用了@SpringBootApplication or @EnableAutoConfiguration注解,则可以直接在pom文件中添加jasypt-spring-boot依赖,然后就可以在整个Spring环境中使用jasypt对属性进行加解密操作(属性包括:系统属性、环境属性、命令行参数、properties、yml以及任何其他属性源)。
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
方式二:
如果项目中没有使用到@SpringBootApplication or @EnableAutoConfiguration注解,则可以通过以下两个步骤完成对Jasypt的集成。
步骤一:pom文件引入jasypt依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.4</version>
</dependency>
步骤二:在配置类中,添加@EnableEncryptableProperties注解,示例如下:
@Configuration
@EnableEncryptableProperties
public class MyApplication {
...
}
通过这种方式,你的项目一样可以集成Jasypt,并且可加密属性也可以在整个Spring环境中启用(属性包括:系统属性、环境属性、命令行参数、properties、yml以及任何其他属性源)。
方式二:
如果项目中没有使用到@SpringBootApplication or @EnableAutoConfiguration注解,又不想在整个Spring环境中启用加密的属性,则可以使用该种方式,具体步骤如下:
步骤一:pom文件引入jasypt依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.4</version>
</dependency>
步骤二、在配置类中,使用@EncryptablePropertySource注解添加任意数量想要生效加密属性的配置文件路径,与Spring中@PropertySource注解的使用类似,示例如下:
@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
...
}
同时,还可以使用@EncryptablePropertySources 注解对@EncryptablePropertySource配置进行分组,示例如下:
@Configuration
@EncryptablePropertySources({
@EncryptablePropertySource("classpath:encrypted.properties"), @EncryptablePropertySource("classpath:encrypted2.properties")})
public class MyApplication {
...
}
说明:从Jasypt 1.8版本开始,@EncryptablePropertySource注解支持配置YAML文件
springboot整合jasypt
引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
在配置文件中指定自定义加密器的bean
# 指定bean
jasypt:
encryptor:
bean: CodeEncryBean
property:
prefix: DONG(
suffix: )
使用自定义加密器(创建bean)
说明:
使用自定义加密器,这样会更安全,因为如果将加密密钥写在配置文件中,这跟没有加密差不多。
jasypt.encryptor.password=abc(你的密钥)
定义个配置类 MyJasyptConfig.java
package com.example.springbootjasypt.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;
/**
* @author 成大事
* @since 2022/7/16 16:57
*/
@Configuration
public class MyJasyptConfig {
private String key = "PEB123@321BEP";
@Bean(name = "CodeEncryBean")
public StringEncryptor CodeEncryBean() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(key);
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;
}
//测试
public static void main(String[] args) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("PEB123@321BEP");
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);
//===================================
String encrypt = encryptor.encrypt("root");
String encrypt2 = encryptor.encrypt("mysql729");
//String decrypt = encryptor.decrypt("ZxY08m8wOk4qE/cTEgfzhRbYQlxKg5mhG+kZ6P5lc0MQwy87Z3MouPFWyVGlGyPf");
//String decrypt2 = encryptor.decrypt("vtK/ygFJN1togHuUS17IbKsc6QQtyK2L2huyXLMmsc9vRQE0zCU+Qo5zQy0FfkQs");
System.out.println(encrypt);
System.out.println(encrypt2);
//System.out.println(decrypt);
//System.out.println(decrypt2);
}
}
在配置文件中将密码替换
注意:jasypt人家默认是要这样识别 ENC(密文)
例如:
spring:
datasource:
username: ENC(06otKd5RtT8tYtbrxZGPUG3Rc9X85BXa) #数据库的用户名
password: ENC(OkEx54575MtmLRQEmy1yCYitDhLS5Lay9YM+sRIR2Qg=) #数据库的密码
不使用NNC()包裹,自定义
自己定义前后缀
spring:
datasource:
username: DONG(06otKd5RtT8tYtbrxZGPUG3Rc9X85BXa) #数据库的用户名
password: DONG(OkEx54575MtmLRQEmy1yCYitDhLS5Lay9YM+sRIR2Qg=) #数据库的密码
# 指定bean
jasypt:
encryptor:
bean: CodeEncryBean
property:
prefix: DONG(
suffix: )
当然这个加密的密钥还有其他的引入方式,但是感觉这种就很可以了
方式一:直接作为程序启动时的命令行参数来带入
方式二:直接作为程序启动时的应用环境变量来带入
方式三:甚至可以作为系统环境变量的方式来带入–最安全