后端加密JDBC.properties文件

首先创建一个JAVA类,来读取properties文件的需要解密的属性,加密解密使用的AES工具可以直接搜到,做成一个工具类,然后再调用。

DBPropertyPlaceholderConfigurer.java
public class DBPropertyPlaceholderConfigurer  extends  PropertyPlaceholderConfigurer  {
    private String[] encryptedProperties;   // 需要解密的属key
​
    public Properties getProperties() {
        return properties;
    }
​
    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
​
    private Properties properties;  // 配置文件
    public void setEncryptedProperties(String[] encryptedProperties) {
        this.encryptedProperties = encryptedProperties;
    }
    @Override
    protected void convertProperties(Properties properties) {
        if(encryptedProperties != null) {
            // 遍历需要解密的key
            for(int i=0; i < encryptedProperties.length; i++) {
                String key = encryptedProperties[i];
                if(properties.containsKey(key)) {
                    String value = properties.getProperty(key);
                    // 解密
                    value = AESUtil.decrypt(value);
                    // 重新赋值
                    properties.setProperty(key, value);
                }
            }
        }
        this.properties = properties;
        super.convertProperties(properties);
    }
​
​
}

然后在applicationContext.xml里面配置bean

<bean id="placeholderConfigurer"
      class="com.callray.alarm.DBPropertyPlaceholderConfigurer" >
    <property name="order" value="1"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations" value="classpath:jdbc.properties"></property>
    <property name="encryptedProperties">
        <array>
            <value>jdbc.user</value>
            <value>jdbc.password</value>
        </array>
    </property>
</bean>

最后建一个测试类来测试有没有解密成功

public static void main(String[] args)
{
​
   ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
​
   DBPropertyPlaceholderConfigurer bean=(DBPropertyPlaceholderConfigurer)  ctx.getBean("placeholderConfigurer");
​
   System.out.println( bean.getProperties().getProperty("jdbc.user"));
   System.out.println( bean.getProperties().getProperty("jdbc.password"));
}

加密后的配置是这样的

jdbc.user=SnP8wWkIWtwGzjYsRVdSXQ== 
jdbc.password=WgmuiVeZRbergKncCPZvqg==

测试解密后

sa

123456

参考文章:spring项目中对jdbc.properties中的明文密码加密解密_jdbc密码加密_Mentality°的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于外部的application.properties中的jdbc配置进行加密,可以采用如下方法: 首先,需要引入加密工具,例如Jasypt(Java simplified encryption)等。在项目的pom.xml文件中添加对应的依赖项。 然后,在应用的配置文件(如application.properties)中配置数据库连接信息,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=mysecretpassword ``` 接下来,在代码中使用Jasypt对这些敏感信息进行加密和解密。可以创建一个Encryptor类,负责加密和解密操作。 ```java import org.jasypt.encryption.pbe.PBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; public class Encryptor { private static final String ENCRYPT_PASSWORD = "yourEncryptionPassword"; public static void main(String[] args) { // 加密 System.out.println("Encrypted password: " + encrypt("mysecretpassword")); // 解密 System.out.println("Decrypted password: " + decrypt("encryptedPassword")); } private static String encrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.encrypt(value); } private static String decrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.decrypt(value); } } ``` 在配置文件中使用加密后的值,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=ENC(encryptedPassword) ``` 当应用启动时,会自动调用Encryptor类中的encrypt方法对密码进行加密,并将加密后的密码存储在application.properties中。在使用密码时,会自动调用Encryptor类中的decrypt方法进行解密。这样就可以保护数据库密码,避免了明文存储的安全隐患。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值