1.使用环境
springboot:2.1.4.RELEASE
JDK:8
jasypt-spring-boot-starter:3.0.2
Jasypt默认算法为PBEWithMD5AndDES,该算法需要一个加密密钥,可以在应用启动时指定(环境变量)。也可以直接写入配置文件
2.引入依赖
!-- 配置文件加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.加密方式
3.1 application.properties配置文件版
server.port=8080
### mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xx.xx
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=ENC(jGfd15IFYKMjMW5bUYp06BQ8OCdHTXi3zbD4j9tw6U3PPvvA5GS8zn10kgVmgxXY)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#加密盐值,获取加密后的值后删除,在vmOptions配置(如下图)
jasypt.encryptor.password=123adf
@SpringBootTest
@RunWith(SpringRunner.class)
public class D2ApplicationTests {
@Resource
private StringEncryptor jasyptStringEncryptor;
@Test
public void contextLoads() {
String pwd = jasyptStringEncryptor.encrypt("root");
System.out.println("pwd:" + pwd);
}
}
加密后,可删除jasypt.encryptor.password配置;发版时可在命令行中配置
3.2 函数版
// 创建加密对象,默认 PBEWithMD5AndDES
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
// 加密所需的密钥
textEncryptor.setPassword("password");
// 加密后的数据(数据库的用户名或密码)
String encData = textEncryptor.encrypt("Password@1");
// 解密后的数据(原数据)
String decData = textEncryptor.decrypt(encData);
System.out.println("encData: " + encData);
System.out.println("decData: " + decData);
4.所遇问题
org.jasypt.exceptions.EncryptionOperationNotPossibleException:
Encryption raised an exception.
A possible cause is you are using strong encryption algorithms and you have not
installed the Java Cryptography Extension (JCE) Unlimited Strength
Jurisdiction Policy Files in this Java Virtual Machine
解决方案:java密码扩展无限制权限策略文件安装–jce_policy安装
文件下载地址:
下面为下载链接
JDK 1.7–jce安装地址:JDK1.7 -jce地址
JDK 1.8–jce安装地址:JDK 1.8–jce安装地址
相关文件来自于博客:https://www.cnblogs.com/zgngg/p/13859299.html) 感谢博主:疯狂的⑨酱 的无私奉献文章来源:https://www.toymoban.com/news/detail-610088.html
jar配置路径:
JDK:将两个jar文件放到%JDK_HOME%\jre\lib\security下
JRE:将两个jar文件放到%JRE_HOME%\lib\security下文章来源地址https://www.toymoban.com/news/detail-610088.html
到了这里,关于SpringBoot+jasypt-spring-boot-starter实现配置文件明文加密的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!