小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

關(guān)于SSH框架整合中,數(shù)據(jù)庫配置文件加密解決思路記載

 瀚海璨夜 2019-02-26

         在SSH框架項目中,如果遇到客戶需要加密數(shù)據(jù)庫配置文件(jdbc.properties等),規(guī)定用戶名或者密碼不能以明文的形式出現(xiàn)在配置文件中。該問題可以通過重寫spring的processProperties方法來實現(xiàn),解決方法如下:

1、首先確定加密/解密算法,這里以DES算法為例,加密/解密算法較為簡單,這里就不贅述了:

加密:

public class Encryption {
    /**
     * DES算法密鑰
     */  
    private static final byte[] DES_KEY = { xxx,-xxx,xxx,-xxx,xxx,-xxx,xxx,-xxx};  
    /**
     * 數(shù)據(jù)加密,算法(DES)
     *
     * @param data
     *            要進行加密的數(shù)據(jù)
     * @return 加密后的數(shù)據(jù)
     */  
    public static String encryptBasedDes(String data) {  
        String encryptedData = null;  
        try {  
            // DES算法要求有一個可信任的隨機數(shù)源  
            SecureRandom sr = new SecureRandom();  
            DESKeySpec deskey = new DESKeySpec(DES_KEY);  
            // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成一個SecretKey對象  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
            SecretKey key = keyFactory.generateSecret(deskey);  
            // 加密對象  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.ENCRYPT_MODE, key, sr);  
            // 加密,并把字節(jié)數(shù)組編碼成字符串  
            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}; 
    /**
     * 數(shù)據(jù)解密,算法(DES)
     *
     * @param cryptData
     *            加密數(shù)據(jù)
     * @return 解密后的數(shù)據(jù)
     */  
    public static String decryptBasedDes(String cryptData) {  
        String decryptedData = null;  
        try {  
            // DES算法要求有一個可信任的隨機數(shù)源  
            SecureRandom sr = new SecureRandom();  
            DESKeySpec deskey = new DESKeySpec(DES_KEY);  
            // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成一個SecretKey對象  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
            SecretKey key = keyFactory.generateSecret(deskey);  
            // 解密對象  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.DECRYPT_MODE, key, sr);  
            // 把字符串解碼為字節(jié)數(shù)組,并解密  
            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.創(chuàng)建解密類,需要覆蓋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){
            //將密文轉(zhuǎn)換為明文
            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>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多