数据库配置文件用户名和密码加密解密

时间:2025-02-19 07:59:58

import .BASE64Decoder;
import .BASE64Encoder;
import ;
import ;
import ;
import ;

public class Test7 {
    private static Key key;
    private static String KEY_STR = "mykey";

    static {
        try {
            KeyGenerator generator = ("DES");
            SecureRandom secureRandom = ("SHA1PRNG");
            (KEY_STR.getBytes());
            (secureRandom);
            key = ();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //对字符串进行加密,返回BASE64的加密字符串
    public  String getEncryptString(String str){
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try
        {
            byte[] strBytes = ("UTF-8");
            Cipher cipher = ("DES");
            (Cipher.ENCRYPT_MODE, key);
            byte[] encryptStrBytes = (strBytes);
            return (encryptStrBytes);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }

    }

    //对BASE64加密字符串进行解密
    public static String getDecryptString(String str){
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try
        {
            byte[] strBytes = (str);
            Cipher cipher = ("DES");
            (Cipher.DECRYPT_MODE, key);
            byte[] encryptStrBytes = (strBytes);
            return new String(encryptStrBytes,"UTF-8");
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }

    }
}


配置文件加密后引用问题:
配置文件如下:

jdbc-driver=
jdbc-url=jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=UTF-8
jdbc-user=Ov4j7fKiCzY=
jdbc-password=Ov4j7fKiCzY=

第一步:
新建类继承PropertyPlaceholderConfigurer类,重写getDecryptString方法

package ;

import ;
import .BASE64Decoder;
import ;
import ;
import ;
import ;

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private String[] encryptPropNames = {"jdbc-user", "jdbc-password"};
    private static Key key;
    private static String KEY_STR="mykey";

    static{
        try
        {
            KeyGenerator generator = ("DES");
            SecureRandom secureRandom=("SHA1PRNG");
            (KEY_STR.getBytes());
            (secureRandom);
            key = ();
            generator=null;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    private boolean isEncryptProp(String propertyName)
    {
        for (String encryptName : encryptPropNames)
        {
            if ((propertyName))
            {
                return true;
            }
        }
        return false;
    }
    
    public static String getDecryptString(String str){
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try
        {
            byte[] strBytes = (str);
            Cipher cipher = ("DES");
            (Cipher.DECRYPT_MODE, key);
            byte[] encryptStrBytes = (strBytes);
            return new String(encryptStrBytes,"UTF-8");
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected String convertProperty(String propertyName, String propertyValue){
        //如果在加密属性名单中发现该属性
        if (isEncryptProp(propertyName)){
            String decryptValue = getDecryptString(propertyValue);
            (decryptValue);
            return decryptValue;
        }else {
            return propertyValue;
        }
    }
}

第二步:
引用配置文件:

<bean class="" p:location="classpath:/"></bean>
代替:

<context:property-placeholder location="classpath:/" />

第三步:

数据源配置如下:

<!--dbcp数据源,只对当前项目有用-->
    <bean  class="">
        <property name="driverClassName" value="${jdbc-driver}"/>
        <property name="url" value="${jdbc-url}"/>
        <property name="username" value="${jdbc-user}"/>
        <property name="password" value="${jdbc-password}"/>
    </bean>