使用Jasypt对SpringBoot配置文件加密

引用:使用Jasypt加密:https://blog.csdn.net/rongxiang111/article/details/85255370
使用Jasypt对.properties配置文件加密:http://www.voidcn.com/article/p-ermeyjiw-st.html
                                 http://www.voidcn.com/article/p-uasrepgn-xo.html

使用Druid对数据库配置文件加密:https://www.jianshu.com/p/313099298e76

数据库密码加密方案、

一、使用Jasypt加密
1、pom.xml中引入依赖
<!-- jasypt加密依赖 -->
<dependency>
   <groupId>com.github.ulisesbocchio</groupId>
   <artifactId>jasypt-spring-boot-starter</artifactId>
   <version>1.16</version>
</dependency>
2、application.yml配置文件中配置加密所需的salt(盐)
# jasypt用于加密
jasypt:
  encryptor:
    password: xxxxxx
3、加密

新建一个工具类JasyptUtil,main方法中加密代码示例如下:
public static void main(String[] args) {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    //1.加密所需的salt(盐),此处的值要与application中的配置的password一样。
    textEncryptor.setPassword("xxxxxx");
    //2.要加密的数据.运行完main方法后,将打印出的加密内容在application.yml相关参数中替换:
    String password = textEncryptor.encrypt("password_xxxxxx");
    // application.yml中替换:ENC(password)
    System.out.println("password:"+password);
注:每次加密生成的密码均不相同,但解密后密码一致。

4、配置加密数据

将配置文件application.yml中的数据库的密码替换为上述结果,加密字符串要在ENC()中。

二、使用Jasypt对.properties配置文件加密

如果数据库配置数据存放在.properties文件中,无法通过上述方法直接解密,可使用如下两种方案解决:

方案一:重写PropertyPlaceholderConfigurer类中的processProperties方法进行解密
public class PropertyPlaceholderConfigurerExt extends PropertyPlaceholderConfigurer {

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {
        String password = props.getProperty("mall.source.jdbc.password");
        if (password != null) {
            //解密jdbc.password属性值,并重新设置
            props.setProperty("mall.source.jdbc.password", "此处为解密算法生成的字符串");
        }
        super.processProperties(beanFactory, props);

    }
}
注释掉原有的placeholder加载方式,改为自定义的placeholder
 <bean id="propertyConfig" class="com.sogou.mall.source.internal.util.PropertyPlaceholderConfigurerExt">
        <property name="locations">
            <list>
                <value>classpath*:需加密的配置文件</value>
            </list>
        </property>
    </bean>
方案二、使用jasypt为spring相应版本实现的placeholder
  <bean id="environmentVariablesConfiguration"
          class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES"/>-
        <property name="password" value="xxxxxx"/>
    </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>classpath*:需加密的配置文件</value>
            </list>
        </property>
        <property name="fileEncoding" value="utf-8" />
    </bean>
三、使用Druid对数据库配置文件加密

1、创建自己的数据源

新建类SecurityDateSource继承数据源DruidDataSource并重写其中设置用户名和密码的方法:
public class SecurityDateSource extends DruidDataSource{
    @Override
    public void setUsername(String username) {
        try {
            username = ConfigTools.decrypt(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.setUsername(username);
    }

    @Override
    public void setPassword(String password) {
        try {
            password = ConfigTools.decrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.setPassword(password);
    }

    public static void main(String[] args) throws Exception{
        String password = "123456";
        String username = "root";
        System.out.println("加密后的password = [" + ConfigTools.encrypt(password) + "]");
        System.out.println("加密后的username = [" + ConfigTools.encrypt(username) + "]");
    }
}
2、Spring文件中配置数据源
<bean id="dataSource" class="xxx.SecurityDateSource"
    init-method="init" destroy-method="close">
    <property name="url" value="${connection.url}" />
    <property name="username" value="${connection.username}" />
    <property name="password" value="${connection.password}" />
    ...省略其他配置...
</bean>

3、加密配置文件
#MySql数据库配置
connection.username=xxxxxx
connection.password=xxxxxx
 
原文地址:https://www.cnblogs.com/tilamisu007/p/12036591.html