| By Sword.Liue
按照官方的说法:Jasypt(Java Simplified Encryption)是一个Java库,它使开发人员能够以最少的编码向其项目添加基本加密功能,而无需对加密的工作原理有太过深入的了解。
Jasypt (Java Simplified Encryption) is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort and without the need of having deep knowledge on how cryptography works.
如果你使用的是 Sping Boot, 并且开启了自动配置,那么请继续往下看,如果没有,可以直接跳过本节
# 开启了自动配置的 Spring Boot 启动类 # 长这样 @SpringBootApplication ... public class CicishopApp {} # 或者 长这样 @EnableAutoConfiguration ... public class CicishopApp {}
com.github.ulisesbocchio
jasypt-spring-boot-starter
2.1.2
如果你的 Spring Boot 版本是 Version 2.1.X,那么请使用
3.0.X 截至 2020-05-30,Jasypt 的最新 starter 版本是 v3.0.2
Maven Central 也可以找到它([
jasypt-spring-boot-starter
]([https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22com.github.ulisesbocchio%22%20AND%20a%3A%22jasypt-spring-boot-starter%22](https://search.maven.org/classic/#search|gav|1|g%3A"com.github.ulisesbocchio" AND a%3A"jasypt-spring-boot-starter")))的最新版本
application.properties
# 这是为密码加密的盐,配置在这里,告诉 Jasypt 使用该盐对 spring.datasource.password 进行解密
jasypt.encrypt.password: 盐
# 这里是我们配置的密码,ENC() 为固定格式,括号里面是加密后的密码
spring.datasource.password: ENC(这里是加密后的密码)
a. 生成加密密码方法有两大类,一种是在线的,这个比较简单,只需要输入密码和盐,点击生成即刻搞定
这里我提供一个免费的可以在线加密的链接:=》走起
b. 通过 Client ,在命令行生成,这个比较麻烦一点,但是为了安全起见,一般还是会选择通过 Client 生成
这里主要说说通过 Client 的加密方式,请先:下载 jasypt-1.9.3-dist.zip
解压后,进入 /bin 目录,在命令行运行一下命令:
encrypt.bat password=mypassword input=slat
详情可参考 官方文档
将加密后的密码和对应的盐填入 1.2 所示位置,重启项目即可
对于没有使用
@SpringBootApplication
或者@EnableAutoConfiguration
的项目,我们可以手动配置
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.1.2</version>
</dependency>
同 1.2 数据库配置
@Configuration
@EnableEncryptableProperties
public class JasyptConfig {
}
最后,测试搞定。
在 2.3 所示的配置类中加入自定义加密 Bean, 你可以更改所有的可配置属性。
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
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;
/**
* Created by sword on 2020/5/31.
*/
@Configuration
@EnableEncryptableProperties
//@EncryptablePropertySource(name = "EncryptedProperties", value = "application.properties")
public class JasyptConfig {
@Bean(name = "encryptorBean")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("secretkey"); // 这是加密的盐,建议直接硬编码,提高安全性
config.setAlgorithm("PBEWithMD5AndDES"); // 加密算法
config.setKeyObtentionIterations("1000"); // key 迭代次数
config.setPoolSize("1"); // 池大小
config.setProviderName("SunJCE"); // 提供方
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); // 随机盐生成器
config.setStringOutputType("base64"); // 加密后输出字符串编码方式
encryptor.setConfig(config);
return encryptor;
}
}
然后更新配置文件,加密 Bean
只想自定义的 Bean
。
# Passw0rd , salt: secretkey
spring.database.password=ENC(6r+OOzsrKokUmERUF0sNeWd3uPzcJj+H)
# jasypt.encryptor.password=secretkey
# 配置加密类,指向自定义的加密 Bean
jasypt.encryptor.bean=encryptorBean