数据库配置文件jdbc.properties 实现用户名密码加密

9 篇文章 0 订阅

项目框架:spring

我们在开发应用时,需要连接数据库,一般把数据库信息放在一个属性配置文件中,比如jdbc.properties,具体的内容

jdbc.properties配置文件

jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=123456


里面用明文的方式存储着数据库的敏感信息用户名username和密码password,这是不好的行为,容易产生安全问题。那我们如何实现加密存储呢?

1、首先需要找一个对称加密工具进行加密解密:加密工具类

2、把加密后的数据放入到jdbc.properties配置文件中

jbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=73A949DD29845907
jdbc.password=F73556ABB1FB8849D72960B9CC30FF51d


3、自定义PropertyPlaceholderConfigurer类,在读取properties配置文件的时候进行解密

// 配置参数处理
public class DBPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
 
    private String[] encryptedProperties;   // 需要解密的属key
    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 = EncryptionUtil.decrypt(value);
                    // 重新赋值
                    properties.setProperty(key, value);
                }
            }
        }
        this.properties = properties;
        super.convertProperties(properties);
    }
 
    public String getProperty(String key) {
        return this.properties.getProperty(key);
    }
}

 

4、在spring-config.xml配置文件

<!-- 注册自定义PropertyPlaceholderConfigurer类,解密Properties文件 -->
<bean id="placeholderConfigurer"
      class="com.gpdi.mdata.utils.DBPropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:jdbc.properties" />
    <property name="encryptedProperties">
        <array>
            <value>jdbc.username</value>
            <value>jdbc.password</value>
        </array>
    </property>
</bean>
 
<context:property-placeholder location="classpath:jdbc.properties" ignore-resource-not-found="true"/>


注意:先后顺序为先注册PropertyPlaceholderConfigurer类,再是配置文件

 

这里使用的:加密工具类
--------------------- 
原文:https://blog.csdn.net/qq_37776015/article/details/90904606 
 

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Druid实现数据库加密,你需要进行以下步骤: 1. 在Maven中添加Druid的依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 2. 在db.setting中添加Druid的配置信息: ``` # 数据库驱动类 driver=com.mysql.jdbc.Driver # 数据库连接URL url=jdbc:mysql://localhost:3306/test # 数据库用户名 username=root # 数据库密码 password=root # Druid的配置信息 initialSize=5 maxActive=10 minIdle=5 maxWait=30000 filters=stat,wall connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ``` 3. 在代码中使用Druid的数据源和连接池: ```java import com.alibaba.druid.pool.DruidDataSource; import java.io.FileInputStream; import java.util.Properties; public class Main { public static void main(String[] args) { Properties props = new Properties(); try { props.load(new FileInputStream("db.setting")); } catch (Exception e) { e.printStackTrace(); } DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(props.getProperty("url")); dataSource.setUsername(props.getProperty("username")); dataSource.setPassword(props.getProperty("password")); dataSource.setInitialSize(Integer.parseInt(props.getProperty("initialSize"))); dataSource.setMaxActive(Integer.parseInt(props.getProperty("maxActive"))); dataSource.setMinIdle(Integer.parseInt(props.getProperty("minIdle"))); dataSource.setMaxWait(Integer.parseInt(props.getProperty("maxWait"))); dataSource.setFilters(props.getProperty("filters")); dataSource.setConnectionProperties(props.getProperty("connectionProperties")); // 使用dataSource.getConnection()获取数据库连接,进行数据库操作 // ... } } ``` 这样,你就可以使用Druid实现数据库加密了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MichaelYZ111

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

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

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

打赏作者

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

抵扣说明:

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

余额充值