Spring Boot2 使用 Jasypt加密配置文件 idea

1 、首先在xml 配置 以下,
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.8</version>
		</dependency>

		<dependency >
			<groupId> com.github.ulisesbocchio </groupId >
			<artifactId > jasypt-spring-boot-starter </artifactId >
			<version > 2.0.0 </version >
		</dependency >

2、maven down 下以后,配置文件添加 ENC(加密后的数值)加密

jasypt.encryptor.password=security
jasypt.encryptor.algorithm=PBEWithMD5AndDES
spring.datasource.password = ENC(CI/3yFcn6kg/GDlp+miWOw==)

3、BootApp;ication 配置添加注解

@SpringBootApplication
@Configuration //这里是
@EnableEncryptableProperties //这里是

public class BootApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootApplication.class, args);
	}

}

4、加密解密方式

Maven依赖如下:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.0.0</version>
</dependency>
jasypt-spring-boot
如果项目里没有使用@SpringBootApplication或者@EnableAutoConfiguration,可以手动在Configuration类上添加注解@EnableEncryptableProperties,来在整个环境的属性启用属性加密。

Maven依赖如下:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.0.0</version>
</dependency>
使用:

@Configuration
@EnableEncryptableProperties
public class MyApplication {
    ...
}
指定加密属性文件
前面两种方法都会在整个环境中启动属性加密。如果想指定加密文件,可以使用@EncryptablePropertySource指定。

Maven依赖:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.0.0</version>
</dependency>
@EncryptablePropertySource指定文件:

@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
	...
}
@EncryptablePropertySources指定多个文件:

@Configuration
@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
	                             @EncryptablePropertySource("classpath:encrypted2.properties")})
public class MyApplication {
	...
}
如果项目里使用@SpringBootApplication,可以使用@PropertySource指定加密的文件:

@SpringBootApplication
@EnableEncryptableProperties
@PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
        ...
}
使用
生成密文

启动时加载命令模式
 通过命令行运行 jasypt-1.9.2.jar 包命令来加密解密:




1. 在jar包所在目录打开命令行,运行如下加密命令:


[plain] view plain copy
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" 
password=security algorithm=PBEWithMD5AndDES  
运行结果如下:
[plain] view plain copy
----ENVIRONMENT-----------------  
  
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14   
  
----ARGUMENTS-------------------  
  
algorithm: PBEWithMD5AndDES  
input: root  
password: security  
  
----OUTPUT----------------------  
  
i00VogiiZ1FpZR9McY7XNw==  
 2. 使用刚才加密出来的结果进行解密,执行如下解密命令:


[plain] view plain copy
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI 
input="i00VogiiZ1FpZR9McY7XNw==" password=security algorithm=PBEWithMD5AndD  
运行结果如下:


[plain] view plain copy
----ENVIRONMENT-----------------  
  
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14   
  
----ARGUMENTS-------------------  
  
algorithm: PBEWithMD5AndDES  
input: i00VogiiZ1FpZR9McY7XNw==  
password: security  
  
----OUTPUT----------------------  
  
root  
配置文件中使用密文

xxx=ENC(密文)
启动spring boot应用需要--jasypt.encryptor.password配置前面用来加密明文的密码。

java -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=supersecretz

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闫 先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值