为什么80%的码农都做不了架构师?>>>
背景
我们的应用之前使用的是Druid数据库连接池,由于需求我们迁移到HikariCP连接池,druid 数据源加密提供了多种方式:可以在配置文件my.properties中指定config.decrypt=true也可以在DruidDataSource的ConnectionProperties中指定config.decrypt=true也可以在jvm启动参数中指定-Ddruid.config.decrypt=true但是HikariCP默认没有提供实现数据源加解密的方法应用中会存在多个需要配置敏感信息(比如stfp等),都需要加密,类似于druid加解密方式依赖于工具类的实现,没有统一的加解密标准,麻烦、而且不好维护。
Spring Cloud Config 的解决方案
Config Server 加解密依赖JDK的JCE。 JDK8的下载地址:
配置config serveencrypt.key=foo
使用config server 提供的加解密接口生成密文
curl localhost:4001/encrypt -d lengleng密文
配置文件使用密文
spring:datasource:password: '{ciper}密文'xxx: '{ciper}密文'
其他的非对称加密等高级配置,参考官方文档。注意一个bug (No key was installed for encryption service)
jasypt 的解决方案
Maven依赖<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>1.16</version></dependency>
配置
jasypt:encryptor:password: foo #根密码
调用JAVA API 生成密文
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = PigAdminApplication.class)public class PigAdminApplicationTest {@Autowiredprivate StringEncryptor stringEncryptor;@Testpublic void testEnvironmentProperties() {System.out.println(stringEncryptor.encrypt("lengleng"));}}
配置文件中使用密文
spring:datasource:password: ENC(密文)xxx: ENC(密文)
其他非对称等高级配置参考