1. 程式人生 > >springMVC web專案 資料庫使用者名稱密碼加密解密

springMVC web專案 資料庫使用者名稱密碼加密解密

在使用springMVC開發web專案中,資料庫的使用者名稱,密碼一般都是配置在.properties檔案中

然後在通過.xml配置檔案引入.properties的變數,例如

在config.properties檔案中,配置如下變數,變數值配置在pom.xml的profile標籤下,在此就不再贅述

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://${p.jdbc.url}/${p.jdbc.dbname}?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&rewriteBatchedStatements\=true
jdbc.username=${p.jdbc.username}
jdbc.password=${p.jdbc.password}
在applicationContext.xml中,通過如下標籤引入這些變數值
<context:property-placeholder location="classpath:/config.properties" />

這樣對於是明文的帳號,密碼,是沒有問題的。但是如果我在配置檔案中的帳號密碼是加密後的,那麼如何進行使用配置呢?

解決辦法:

1.首先確定加密解密演算法,這種情況下,我們肯定選擇是對稱性加密解密演算法,首選DES演算法,在這裡就拿他舉例

2.完成加密解密演算法,這個程式碼很簡單,就不贅述,金鑰自己決定,保密即可

public class DESUtils
{
    private static Key key;
    private static String KEY_STR="mykey";
    
    static{
        try
        {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(KEY_STR.getBytes());
            generator.init(secureRandom);
            key = generator.generateKey();
            generator=null;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 對字串進行加密,返回BASE64的加密字串
     * <功能詳細描述>
     * @param str
     * @return
     * @see [類、類#方法、類#成員]
     */
    public static String getEncryptString(String str){
        BASE64Encoder base64Encoder = new BASE64Encoder();
        System.out.println(key);
        try
        {
            byte[] strBytes = str.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return base64Encoder.encode(encryptStrBytes);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        
    }
    
    /**
     * 對BASE64加密字串進行解密
     * <功能詳細描述>
     * @param str
     * @return
     * @see [類、類#方法、類#成員]
     */
    public static String getDecryptString(String str){
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try
        {
            byte[] strBytes = base64Decoder.decodeBuffer(str);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return new String(encryptStrBytes,"UTF-8");
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        
    }
    
    
    public static void main(String[] args)
    {
        String name ="root";
        String password="1234";
        String encryname = getEncryptString(name);
        String encrypassword = getEncryptString(password);
        System.out.println(encryname);
        System.out.println(encrypassword);
        
        System.out.println(getDecryptString(encryname));
        System.out.println(getDecryptString(encrypassword));
    }
}
3.springMVC中需要實現解密的介面

需要覆蓋convertProperty方法,encryptPropNames儲存的是需要解密的屬性

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer
{
    private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};
    
    @Override
    protected String convertProperty(String propertyName, String propertyValue)
    {
        
        //如果在加密屬性名單中發現該屬性
        if (isEncryptProp(propertyName))
        {
            String decryptValue = DESUtils.getDecryptString(propertyValue);
            System.out.println(decryptValue);
            return decryptValue;
        }else {
            return propertyValue;
        }
        
    }
    
    private boolean isEncryptProp(String propertyName)
    {
        for (String encryptName : encryptPropNames)
        {
            if (encryptName.equals(propertyName))
            {
                return true;
            }
        }
        return false;
    }
}
4.配置這個解密類
用
<bean class="com.cyou.web.common.EncryptPropertyPlaceholderConfigurer" p:location="classpath:/config.properties"></bean>
代替
<context:property-placeholder location="classpath:/config.properties" />