springboot的配置加密(三)之Jasypt

背景

对于前两种分别存在的问题 有其他组件可以负责处理

https://github.com/ulisesbocchio/jasypt-spring-boot

Jasypt Spring Boot provides Encryption support for property sources in Spring Boot Applications.
There are 3 ways to integrate jasypt-spring-boot in your project:

  • Simply adding the starter jar jasypt-spring-boot-starter to your classpath if using @SpringBootApplication or @EnableAutoConfiguration will enable encryptable properties across the entire Spring Environment
  • Adding jasypt-spring-boot to your classpath and adding @EnableEncryptableProperties to your main Configuration class to enable encryptable properties across the entire Spring Environment
  • Adding jasypt-spring-boot to your classpath and declaring individual encryptable property sources with @EncrytablePropertySource

分析

springboot大法好~

其在maven仓库提供了多个版本的jar以供选择

00f7d3938d5eafaf02b1576634ebeb766e1.jpg

对于我们使用jdk7的来说也只能选择1.5.java7版本呢~

正如简介所描述的这般 只要引入了stater就很方便的集成了该功能~SpringBoot之自动配置

zeus项目中也继承了该功能

1
2
3
4
5
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.5-java7</version>
</dependency>

1
2
spring.datasource.password=ENC(zjdITWU2MWpHaOdcF8nG+w==)
jasypt.encryptor.password=f6car

其中jasypt.encryptor.passwordb必填为加解密所需要的密钥

而需要解密的配置使用ENC()将其包围即可~

如何获得密文???

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class JasyptTest {
@Autowired
private StringEncryptor stringEncryptor;
@Test
public void testEnvironmentProperties() {
System.out.println(stringEncryptor.encrypt("qixiaobo"));
}
}

可以得到密文~~~然后使用ENC()包裹起来即可~

弊端

微服务这么多 每个都得要如厮配置

#