jasypt集成spring-boot原理解析

背景

每个应用都有很多配置项,有些配置项对外非常敏感,例如数据库连接密码、私钥等。使用明文存在泄露的风险,生产环境要配合加密算法。jasypt是一个方便、流行的加密工具,支持PBE、AEC和对称加密。它与spring-boot的集成度很高,可以方便的为spring-boot属性进行加解密。

使用方式

要使用jayspt要引入pom文件:

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

以PBE加密为例,要先初始化Encryptor 对象,然后对“abc”进行加解密。

public class EncryptionDemo {
    public static void main(String[] args) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("123");
        encryptor.setConfig(config);

        String rawMsg = "abc";
        String encryptedMsg = encryptor.encrypt(rawMsg);
        System.out.println(rawMsg + " = " + encryptedMsg);
				// abc = tgjLqtpTOf7z6IC+xE9r+w==

        String decryptedMsg = encryptor.decrypt(encryptedMsg);
        System.out.println(encryptedMsg + " = " + decryptedMsg);
				// tgjLqtpTOf7z6IC+xE9r+w== = abc
    }
}

可以看到“abc”加密后结果是“tgjLqtpTOf7z6IC+xE9r+w==”,解密后还是“abc”。并且同样的“abc”参数,每次加密的结果都不一样,但解密的结果是一样的。

集成spring-boot

加解密功能已经有了,接下来怎么集成到spring-boot上,将spring-boot里的配置项自动解密?

由于spring-boot的配置项是放在PropertySource里,jasypt通过生成PropertySource的代理对象。后面从PropertySource获取属性时,先通过jasypt先解密,再返回解密后的值。

下面再看一下jasypt具体是怎么实现的代理,先引入对应的pom文件:

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

生成代理对象

这个starter通过SPI的方式,自动加载了EnableEncryptablePropertiesConfiguration配置类,这个类里注册了jasypt自己的BeanFactoryPostProcessor 。它里面的功能就是拿到ConfigurableEnvironment 里的MutablePropertySourcesMutablePropertySources 里包含所有的PropertySource。把PropertySource包装成自己的EncryptablePropertySource ,然后替换掉原来的PropertySource。

// com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter
public void convertPropertySources(MutablePropertySources propSources) {
    propSources.stream()
            .filter(ps -> !(ps instanceof EncryptablePropertySource))
						// makeEncryptable方法会将PropertySource替换成EncryptablePropertySource的子类
            .map(this::makeEncryptable)
            .collect(toList())
            .forEach(ps -> propSources.replace(ps.getName(), ps));
}

// 这里还分两种代理方式,一种是Proxy,一个是Wrapper
private <T> PropertySource<T> convertPropertySource(PropertySource<T> propertySource) {
    return interceptionMode == InterceptionMode.PROXY
            ? proxyPropertySource(propertySource) : instantiatePropertySource(propertySource);
}

// Proxy会通过Spring的AOP生成代理对象
private <T> PropertySource<T> proxyPropertySource(PropertySource<T> propertySource) {
    //can't be proxied with CGLib because of methods being final. So fallback to wrapper for Command Line Arguments only.
    if (CommandLinePropertySource.class.isAssignableFrom(propertySource.getClass())
            // Other PropertySource classes like org.springframework.boot.env.OriginTrackedMapPropertySource
            // are final classes as well
            || Modifier.isFinal(propertySource.getClass().getModifiers())) {
        return instantiatePropertySource(propertySource);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTargetClass(propertySource.getClass());
    proxyFactory.setProxyTargetClass(true);
    proxyFactory.addInterface(EncryptablePropertySource.class);
    proxyFactory.setTarget(propertySource);
    proxyFactory.addAdvice(new EncryptablePropertySourceMethodInterceptor<>(propertySource, propertyResolver, propertyFilter));
    return (PropertySource<T>) proxyFactory.getProxy();
}

// Wrapper会跟进原始的PropertySource类型,生成对应的包装类
private <T> PropertySource<T> instantiatePropertySource(PropertySource<T> propertySource) {
    PropertySource<T> encryptablePropertySource;
    if (needsProxyAnyway(propertySource)) {
        encryptablePropertySource = proxyPropertySource(propertySource);
    } else if (propertySource instanceof SystemEnvironmentPropertySource) {
        encryptablePropertySource = (PropertySource<T>) new EncryptableSystemEnvironmentPropertySourceWrapper((SystemEnvironmentPropertySource) propertySource, propertyResolver, propertyFilter);
		} else if (propertySource instanceof MapPropertySource) {
        encryptablePropertySource = (PropertySource<T>) new EncryptableMapPropertySourceWrapper((MapPropertySource) propertySource, propertyResolver, propertyFilter);
    } else if (propertySource instanceof EnumerablePropertySource) {
        encryptablePropertySource = new EncryptableEnumerablePropertySourceWrapper<>((EnumerablePropertySource) propertySource, propertyResolver, propertyFilter);
    } else {
        encryptablePropertySource = new EncryptablePropertySourceWrapper<>(propertySource, propertyResolver, propertyFilter);
    }
    return encryptablePropertySource;
}

后面从MutablePropertySources 获取属性的时候,实际上调用的是EncryptablePropertySourceObject getProperty(String name) 方法。

解析属性流程

以applicaiton.properties文件里的配置项为例,它是OriginTrackedMapPropertySource类型,属于MapPropertySource 的子类,会被jasypt包装成EncryptableMapPropertySourceWrapper

想在获取OriginTrackedMapPropertySource 里的属性,会先调用EncryptableMapPropertySourceWrappergetProperty(String name) 方法。

EncryptableMapPropertySourceWrapper 会将请求转交给代理对象CachingDelegateEncryptablePropertySource 。因为CachingDelegateEncryptablePropertySource 会将解析的结果放到Map<String, CachedValue> cache 缓存对象里,避免重复解析。

然后,CachingDelegateEncryptablePropertySource 会将加密内容交给DefaultLazyPropertyResolver 解密配置项。从名字就可以看出,DefaultLazyPropertyResolver 是为了延迟初始化,实际上它包装了DefaultPropertyResolver ,这是默认jasypt的解析器。

DefaultPropertyResolver 先加加密内容执行spring的占位符解析,例如解析${my.password},得到“ENC(tgjLqtpTOf7z6IC+xE9r+w==)”,再对”ENC(tgjLqtpTOf7z6IC+xE9r+w==)”进行解密。

DefaultPropertyResolver 内部调用DefaultLazyEncryptor ,通过延迟加载的方式得到默认的加密机类型。这时会根据上下文配置,选择是使用PBE、AEC还是对称加密。

// com.ulisesbocchio.jasyptspringboot.configuration.StringEncryptorBuilder
public StringEncryptor build() {
		// 包含jasypt.encryptor.password就使用PBE
    if (isPBEConfig()) {
        return createPBEDefault();
		// 包含jasypt.encryptor.privateKeyString相关的配置就使用非对称加密
    } else if (isAsymmetricConfig()) {
        return createAsymmetricDefault();
		// 包含jasypt.encryptor.gcmSecretKeyString相关配置就使用GCM加密
    } else if (isGCMConfig()) {
        return createGCMDefault();
    } else {
        throw new IllegalStateException("either '" + propertyPrefix + ".password', one of ['" + propertyPrefix + ".private-key-string', '" + propertyPrefix + ".private-key-location'] for asymmetric encryption, or one of ['" + propertyPrefix + ".gcm-secret-key-string', '" + propertyPrefix + ".gcm-secret-key-location', '" + propertyPrefix + ".gcm-secret-key-password'] for AES/GCM encryption must be provided for Password-based or Asymmetric encryption");
    }
}

以PBE为例,这里方法的就是PooledPBEStringEncryptor 对象。它内部有多个StandardPBEStringEncryptor 组成一个池子,可以提升加密性能。最后会按顺序获得一个StandardPBEStringEncryptor ,调用decrypt(final String encryptedMessage) 方法进行解密。

总结

jasypt通过BeanFactoryPostProcessor 代理了所有的PropertySource,在获取属性之前,对属性进行解密。过程中涉及到了几个对象:

  • EncryptablePropertySource 是PropertySource的包装类
  • CachingDelegateEncryptablePropertySource 缓存了解密结果
  • EncryptableMapPropertySourceWrapperMapPropertySource 的包装类
  • DefaultPropertyResolver 先解析Spring的占位符
  • DefaultLazyPropertyResolver 实现了延迟加载DefaultPropertyResolver 对象
  • StandardPBEStringEncryptor 是PBE加密机
  • PooledPBEStringEncryptor 通过池化StandardPBEStringEncryptor 提升并发度

引用

jasypt的github:https://github.com/ulisesbocchio/jasypt-spring-boot?tab=readme-ov-file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李昂的数字之旅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值