SpringBoot系列9-使用jasypt自定义stater运行时动态传入加密密码

文章目录

  1. 新建springboot-encryption-configuration项目实现stater
  2. pom文件引入jasypt
  3. 在resources/support/下配置application.properties文件
  4. 在resources/support/下根据不同环境配置文件,此处我的开发和测试环境application-${spring.profiles.active}.properties文件的加密内容一样。请你根据你的情况来配置
  5. 定义SupportConfiguration.java
  6. 将上面项目安装到maven库
  7. 新建springboot-encryption-configuration-demo测试stater
  8. pom文件引入stater依赖
  9. 编写测试类DemoApplicationTests.java
  10. 运行
  11. 大功告成:运行测试类DemoApplicationTests的contextLoads方法console打印出原内容如下

介绍如何自定义一个stater将jasypt封装为一个stater供多个项目使用,同时运行时动态传入加密密码

新建springboot-encryption-configuration项目实现stater

pom文件引入jasypt

   <dependencies>
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
   </dependencies>

说明:

spring-boot-starter 自定义stater引入则具备stater的能力;

jasypt-spring-boot-starter自定义stater引入则具备加密解密能力

在resources/support/下配置application.properties文件

jasypt.encryptor.password=${PWD}

说明:接收运行时传入的解密密码

在resources/support/下根据不同环境配置文件,此处我的开发和测试环境application-${spring.profiles.active}.properties文件的加密内容一样。请你根据你的情况来配置

application-dev.properties和application-test.properties

u.username=ENC(uF42uO9PxXDQYtnYe5++ebszGCkWXdY1NynJ+5Lptsg=)
u.pwd=ENC(+v5XN2Tv+d6VDfpeapME+S6vw2nOfE9L/1sjh6UFzso=)

定义SupportConfiguration.java

/**
 * @author tiankonglanlande
 * @description
 * @createTime 2018 - 11 - 20 11:53
 */
@EnableEncryptableProperties
@EncryptablePropertySources({@EncryptablePropertySource(value = "classpath:support/application.properties", ignoreResourceNotFound = true),
        @EncryptablePropertySource(value = "classpath:support/application-${spring.profiles.active}.properties", ignoreResourceNotFound = true)})
public class SupportConfiguration {

}

说明:

@EnableEncryptableProperties启用属性加密

@EncryptablePropertySource指定加密文件(这里的application-${spring.profiles.active}.properties是根据环境动态加载配置文件)

将上面项目安装到maven库

我这里是使用的idea工具选择Maven Project–>springboot-encryption-configuration –> clean –> 然后compile –> 最后install

新建springboot-encryption-configuration-demo测试stater

pom文件引入stater依赖

    <!--引入stater-->
    <dependency>
        <groupId>com.tiankonglanlande.cn.springboot.stater</groupId>
        <artifactId>springboot-encryption-configuration-stater</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

编写测试类DemoApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
	@Value("${u.username}")
	private String username;
	@Value("${u.pwd}")
	private String pwd;

	@Test
	public void contextLoads() {
		System.out.println("读取username:"+username);
		System.out.println("读取pwd:"+pwd);

	}
}

运行

说明: 点击运行左侧栏的绿色按钮:此时会报错

idea选择 Edit Configurations–> JUnit –>选择 DemoApplicationTests.contextLoads –> 在右边的VM options 填入启动的环境和加密密码 -Dspring.profiles.active=test -DPWD=tiankonglanlande

大功告成:运行测试类DemoApplicationTests的contextLoads方法console打印出原内容如下

读取username:tiankonglanlande
读取pwd:tiankonglanlande_pwd

源码下载链接

原文链接: http://www.lskyf.com/view/30

作者:天空蓝蓝的,版权所有,欢迎保留原文链接进行转载:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值