对jdbc.properties中的密码加密

为什么对jdbc文件加密:如果jdbc中的用户名和密码配置为明文  这样别人就很容易连接上服务器  为了安全考虑 将jdbc中的密码配成加密文件

一、创建加密和解密的Util

二、当我们在spring中配置了jdbc的连接信息后 在这之前我们要将密码解密  然后再进行数据库的连接

    <!-- 密文  此段代码要加在连接数据库之前-->
     <bean id="propertyConfigurerS"  //id   自定义  class为PasswordEncryptConfigurer的路径
          class="com.aa.app.cib.commonUtil.PasswordEncryptConfigurer"> 
          <property name="ignoreUnresolvablePlaceholders" value="true"/>
             <property name="ignoreResourceNotFound" value="true"/>
             <property name="locations">
               <list>
                  <value>/WEB-INF/jdbc.properties</value>  //这是jdbc的路径
               </list>
             </property>
     </bean> 

注意:如果要生成密文 请到对应的Util生成  

话不多说,下面上代码

注意:在jdbc.properties中  密文前面要加上{DES}  否则就不会作为密文解析

package com.jdbcDes;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PasswordEncryptConfigurer extends PropertyPlaceholderConfigurer {

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
        System.out.println("正在解密系统文件...");
        try {
            String JdPassword = props.getProperty("jdbc.password");
            // rk----------------------
            if (JdPassword != null &&JdPassword .startsWith("{DES}")) {

               JdPassword = JdPassword .substring("{DES}".length());
                //解密  password
                JdPassword = EncryptUtil.decodeString(JdPassword);
            }

           //将解密后的密码放入Properties中
            props.setProperty("jdbc.password", JdPassword);
             super.processProperties(beanFactory, props);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BeanInitializationException(e.getMessage());
        }
    }
}

 

然后用DES加密,或者其他的加密工具


 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值