1 依赖工具
- Maven
- Git
- JDK
- IntelliJ IDEA
2 源码拉取
git clone git://github.com/ulisesbocchio/jasypt-spring-boot.git
cd jasypt-spring-boot/
# 3.0.4是最新的版本
git checkout jasypt-spring-boot-parent-3.0.4
mvn clean install
3 jasypt-spring-boot-demo
System.setProperty("jasypt.encryptor.password", "password");
application.yml中的itemConfig.password:ENC(nrmZtkF7T0kjG/VodDvBw93Ct8EgjCA+)
encrypted.properties
secret.property=ENC(nrmZtkF7T0kjG/VodDvBw93Ct8EgjCA+)
启动DemoApplication,可以看到ENC()中的值,被解密出来了
4 源码
4.1 项目结构
项目代码相对1.0复杂了很多,对代码进行拆分,分类
4.2 初始化
通过sprng boot spi引入配置,通过BeanFactoryPostProcessor.postProcessBeanFactory代理PropertySources。getProperty调用StringEncryptor的decrypt方法进行解密
标红的为新增的。
JasyptSpringCloudBootstrapConfiguration:与JasyptSpringBootAutoConfiguration相同代码,但是作用在SpringCloudBootstrap
EncryptablePropertyResolverConfiguration: 提供EncryptablePropertyResolver
CachingConfiguration: 提供RefreshScopeRefreshedEventListener,用于配置刷新
BeanFactoryPostProcessors调用序列图如下
4.3 调用
默认使用wrapper包装器模式。getProperty调用StringEncryptor的decrypt方法进行解密
EncryptablePropertyFilter:属性名的黑白名单过滤
EncryptablePropertyDetector: 是否加密、取出加密的值
EncryptablePropertyResolver:解析加密的property,最终调用StringEncryptor的decrypt解密
4.4 RefreshScopeRefreshedEventListener
默认接收下面的事件。也可以通JasyptEncryptorConfigurationProperties#refreshedEventClasses添加。
"org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent",
"org.springframework.cloud.context.environment.EnvironmentChangeEvent",
"org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent"