打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于SSH框架整合中,数据库配置文件加密解决思路记载

         在SSH框架项目中,如果遇到客户需要加密数据库配置文件(jdbc.properties等),规定用户名或者密码不能以明文的形式出现在配置文件中。该问题可以通过重写spring的processProperties方法来实现,解决方法如下:

1、首先确定加密/解密算法,这里以DES算法为例,加密/解密算法较为简单,这里就不赘述了:

加密:

public class Encryption {
    /**
     * DES算法密钥
     */  
    private static final byte[] DES_KEY = { xxx,-xxx,xxx,-xxx,xxx,-xxx,xxx,-xxx};  
    /**
     * 数据加密,算法(DES)
     *
     * @param data
     *            要进行加密的数据
     * @return 加密后的数据
     */  
    public static String encryptBasedDes(String data) {  
        String encryptedData = null;  
        try {  
            // DES算法要求有一个可信任的随机数源  
            SecureRandom sr = new SecureRandom();  
            DESKeySpec deskey = new DESKeySpec(DES_KEY);  
            // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
            SecretKey key = keyFactory.generateSecret(deskey);  
            // 加密对象  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.ENCRYPT_MODE, key, sr);  
            // 加密,并把字节数组编码成字符串  
            encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes()));  
        } catch (Exception e) {  
//            log.error("加密错误,错误信息:", e);  
            throw new RuntimeException("加密错误,错误信息:", e);  
        }  
        return encryptedData;  
    }  
}

解密:

/**
 * 解密
 * */
public class Decryption {
    /**
     * DES算法密钥
     */  
    private static final byte[] DES_KEY = { xxx,-xxx,xxx,-xxx,xxx,-xxx,xxx,-xxx}; 
    /**
     * 数据解密,算法(DES)
     *
     * @param cryptData
     *            加密数据
     * @return 解密后的数据
     */  
    public static String decryptBasedDes(String cryptData) {  
        String decryptedData = null;  
        try {  
            // DES算法要求有一个可信任的随机数源  
            SecureRandom sr = new SecureRandom();  
            DESKeySpec deskey = new DESKeySpec(DES_KEY);  
            // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
            SecretKey key = keyFactory.generateSecret(deskey);  
            // 解密对象  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.DECRYPT_MODE, key, sr);  
            // 把字符串解码为字节数组,并解密  
            decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));  
        } catch (Exception e) {  
//            log.error("解密错误,错误信息:", e);  
            throw new RuntimeException("解密错误,错误信息:", e);  
        }  
        return decryptedData;  
    }
    
    public static void main(String[] args)
    {
        String str = Decryption.decryptBasedDes("Decryption");
        System.out.println(str);
    }
}

2.创建解密类,需要覆盖processProperties方法

/**
 *
 * @Title: JdbcDecryptPropertiesFile.java   
 * @Package com.xxx.common.util   
 * @Description: 重写spring的processProperties方法,以便在使用配置之前解密
 * @author wsk   
 * @date 2016-6-12 下午3:28:45   
 * @version V1.0
 */
public class JdbcDecryptPropertiesFile extends PropertyPlaceholderConfigurer{

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException{
        //读取配置文件中的密文
        String password = props.getProperty("jdbc.password");
        //避免包之间的依赖
        if(null != password && "" != password && "null" != password){
            //将密文转换为明文
            String decPassword = Decryption.decryptBasedDes(password);
            //将解密后的密码放入property对象中
            props.setProperty("jdbc.password", decPassword);
        }
        super.processProperties(beanFactory, props);
    }
}

3.在spring配置使用自己的解密类

<bean class="com.xxx.common.util.JdbcDecryptPropertiesFile">
           <property name="locations">
               <value>classpath:jdbc.properties</value>
           </property>  
 </bean>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java与Delphi交叉DES加解密的问题
SpringBoot+Shiro学习之密码加密和登录失败次数限制 | z77z的小码窝 | 年少无为,卖码为生。
3DES加密算法
Java家技术论坛 :: 阅读主题 - 用DES加密算法保护Java源代码
利用DES加密算法保护Java源代码
JAVA中怎么搞定DES算法 完整页
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服