对jdbc.properties中的密码加密

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 07:25   28   0

为什么对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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP