Spring 中使用jasypt对配置文件(.properties)中密码加密

本文介绍如何在Spring框架中利用Jasypt框架加密配置文件中的敏感信息,如密码,避免明文显示。通过配置StandardPBEStringEncryptor和EncryptablePropertyPlaceholderConfigurer,实现对.properties文件中密码的加密与解密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring配置中经常使用placeholder来加载一个应用配置文件(.properties),但是其中的各种密码以明文显示出来总该是不好。

不过可以利用jasypt这个框架来扩展这个加密功能,需要用到jasypt中的icu4j-version.jar、jasypt-version-lite.jar、jasypt-version.jar和jasypt-spring31-version.jar

首先,注释掉原有的placeholder加载方式

<!--<context:property-placeholder location="/WEB-INF/config.properties"/> -->
然后使用jasypt为spring相应版本实现的placeholder
<!-- decrypt password in config.properties -->
    <bean id="environmentVariablesConfiguration"
        class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES" />
        <property name="password" value="root" />
    </bean>
    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="locations">
            <list>
                <value>/WEB-INF/config.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="utf-8" />
    </bean> 
最后,修改.properties配置中的明文密码为密文,这个需要自己写一个main方法
public static void main(String[] args) {
        //PBEWithMD5AndDES
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword("root");
        String encrypted = encryptor.encrypt("xxxx");
        System.out.println(encrypted);
    }
然后将输出的密文替换原来的密码
jdbc.password=ENC(jHv0WdiTLJFmOO08RQtUpg==)
这样的密文虽然还是很容易被decode出来,但终究不是明文显示。

个人认为最好的办法可能是 自己去实现一个spring的 place holder,利用md5来匹配配置文件中的密文是否正确。
 ———————————————— 
版权声明:本文为优快云博主「dyyaries」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:

### 查找 `EncryptablePropertySourcesPlaceholderConfigurer` 类的源码 为了找到 `EncryptablePropertySourcesPlaceholderConfigurer` 类的源码,可以从项目的GitHub仓库入手。此项目位于指定的GitCode地址[^4]。 通常,在Spring Boot集成中,此类会作为配置类的一部分来处理占位符解析,并确保属性在加载过程中被正确解密。具体来说: - 此类继承自 `org.springframework.context.support.PropertySourcesPlaceholderConfigurer` 并扩展其功能以支持加密属性。 - 它利用了Jasypt提供的 `StringEncryptor` 接口实现来进行实际的解密操作[^2]。 #### 关键特性 该类的主要职责是在应用程序上下文初始化期间替换配置文件中的占位符值。对于由 `${...}` 表达式的占位符表示的任何加密字符串,`EncryptablePropertySourcesPlaceholderConfigurer` 将调用相应的解密逻辑将其转换为明文形式[^1]。 ```java public class EncryptablePropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer { private final StringEncryptor encryptor; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { super.processProperties(beanFactory, new EncryptableProperties(encryptor, props)); } // Other methods and configurations... } ``` 上述代码片段展示了如何重写父类的方法以便于使用加密处理器包装原始属性对象。这允许框架内部机制透明地访问已解密的数据而无需额外编码工作。 #### 加载外部资源 如果指定了外部属性文件的位置(例如通过 `jasypt.properties.file` 参数),那么这些位置也会受到同样的处理流程影响,即其中包含的所有敏感数据都将按照定义的安全策略进行保护和恢复[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值