mysql數據庫連接賬戶使用加密的方式


 

整體流程

  ①在在配置文件applicatiooncontext中設置自定義的配置文件解析的屬性,

<bean id="appPropertyConfigurer"
        class="com.fjep.common.PropertyConfigurer"
        abstract="false" lazy-init="default" autowire="default">

        <property name="locations">
            <list>
                <value>classpath:/config/database.properties</value>
            </list>
        </property>
        <property name="decodePropertyNames">
           <list>
             <value>database.dev.username</value>
             <value>database.dev.password</value>

           </list>
        </property>
    </bean>

 

  ②PropertyConfigurer工具類中會判斷decodePropertyNames的value值是否與PropertyNames中的名稱一致,如果一致,就會進行解密操作

  ③使用MessageDigestCodingUtil工具類進行加密和解密,加密是先進行base64加密,在進行16位加密;解密反過來,先解16位,在進行64位的解密。

 

 1.Base64的加密類

package com.fjep.utils;
/**
 *說明:64位加密算法
 */
public class Base64 {

    private static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
    private static byte codes[];

    public Base64() {
    }

    public static byte[] decode(char data[]) {
        int tempLen = data.length;
        for (int ix = 0; ix < data.length; ix++) {
            if (data[ix] > '\377' || codes[data[ix]] < 0) {
                tempLen--;

            }
        }
        int len = (tempLen / 4) * 3;
        if (tempLen % 4 == 3) {
            len += 2;
        }
        if (tempLen % 4 == 2) {
            len++;
        }
        byte out[] = new byte[len];
        int shift = 0;
        int accum = 0;
        int index = 0;
        for (int ix = 0; ix < data.length; ix++) {
            int value = data[ix] <= '\377' ? ( (int) (codes[data[ix]])) : -1;
            if (value >= 0) {
                accum <<= 6;
                shift += 6;
                accum |= value;
                if (shift >= 8) {
                    shift -= 8;
                    out[index++] = (byte) (accum >> shift & 0xff);
                }
            }
        }

        if (index != out.length) {
            throw new Error("Miscalculated data length (wrote " + index + " instead of " + out.length + ")");
        } else {
            return out;
        }
    }

    public static char[] encode(byte data[]) {
        char out[] = new char[ ( (data.length + 2) / 3) * 4];
        int i = 0;
        for (int index = 0; i < data.length; index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = 0xff & data[i];
            val <<= 8;
            if (i + 1 < data.length) {
                val |= 0xff & data[i + 1];
                trip = true;
            }
            val <<= 8;
            if (i + 2 < data.length) {
                val |= 0xff & data[i + 2];
                quad = true;
            }
            out[index + 3] = alphabet[quad ? val & 0x3f : 64];
            val >>= 6;
            out[index + 2] = alphabet[trip ? val & 0x3f : 64];
            val >>= 6;
            out[index + 1] = alphabet[val & 0x3f];
            val >>= 6;
            out[index] = alphabet[val & 0x3f];
            i += 3;
        }

        return out;
    }

    static {
        codes = new byte[256];
        for (int i = 0; i < 256; i++) {
            codes[i] = -1;

        }
        for (int i = 65; i <= 90; i++) {
            codes[i] = (byte) (i - 65);

        }
        for (int i = 97; i <= 122; i++) {
            codes[i] = (byte) ( (26 + i) - 97);

        }
        for (int i = 48; i <= 57; i++) {
            codes[i] = (byte) ( (52 + i) - 48);

        }
        codes[43] = 62;
        codes[47] = 63;
    }

    public static void main(String[] args) {

        try {
            String encode = new String(Base64.encode("ioffice".getBytes()));
            System.out.println(encode);
            System.out.println(new String(Base64.decode("5a6d706c634441774d413d3d".toCharArray())));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 2.具體加密解密的工具類MessageDigestCodingUtil

package com.fjep.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/**
 *說明:進行數值摘要的加密和解密處理
 */
public class MessageDigestCodingUtil {
    
    /**
     * 
     *摘要:加密操作
     *@說明:先進行64位的加密 再繼續16進制的加密
     *@param encode
     *@return      
     */
    public static String encode(String encode)
    {
        encode=new String(Base64.encode(encode.getBytes()));
        encode=new String(Hex.encodeHex(encode.getBytes()));
        return encode;
    }
    
    /**
     * 
     *摘要:解密操作
     *@說明:先進行16進制的解密,再進行64位的解密
     *@param decode
     *@return 
     */
    public static String decode(String decode)
    {
        try {
            decode=new String(Hex.decodeHex(decode.toCharArray()));
            decode=new String(Base64.decode(decode.toCharArray()));
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        return decode;
    }
    
    public static void main(String[] args)
    {
        //String str=MessageDigestCodingUtil.encode("jdbc:oracle:thin:@10.188.80.98:1521:dev");
        //System.out.println("encrypt="+MessageDigestCodingUtil.encode("root"));
        System.out.println("encrypt1="+MessageDigestCodingUtil.decode("5a6d706c634639335a57493d"));
//        System.out.println("encrypt="+MessageDigestCodingUtil.decode("5a6d706c634441774d413d3d"));
    }

}

  

 3.對配置文件操作的類PropertyConfigurer

package com.fjep.common;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.ObjectUtils;
import com.fjep.utils.MessageDigestCodingUtil;


/**
 *說明:對.properties文件進行解析操作
 */
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
    private static final Log log=LogFactory.getLog(PropertyConfigurer.class);

    /**
     * 需要進行解密的屬性名稱 decodePropertyNames
     * <list>
     *   <value>zbs</value>
     *   <value>zbs2</value>
     * </list>
     */
    private List<String> decodePropertyNames;
    
    private Properties springPropertiesDatas=null;
    
    @SuppressWarnings("unchecked")
    @Override
    protected void convertProperties(Properties props) {
        Enumeration propertyNames = props.propertyNames();
        while (propertyNames.hasMoreElements()) {
            String propertyName = (String) propertyNames.nextElement();
            String propertyValue = props.getProperty(propertyName);
            String convertedValue=null;
            if(this.decodePropertyNames!=null&&decodePropertyNames.size()>0)
            {
              if(this.decodePropertyNames.contains(propertyName))
                {
                  convertedValue=MessageDigestCodingUtil.decode(propertyValue);
                }else
                  {
                    convertedValue = convertPropertyValue(propertyValue);
                  }
            }else
                 convertedValue = convertPropertyValue(propertyValue);
            if (!ObjectUtils.nullSafeEquals(propertyValue, convertedValue)) {
                props.setProperty(propertyName, convertedValue);
            }
        }
    }

    public List<String> getDecodePropertyNames() {
        return decodePropertyNames;
    }

    public void setDecodePropertyNames(List<String> decodePropertyNames) {
        this.decodePropertyNames = decodePropertyNames;
    }
    
    public String getPropertyValue(String key)
    {
        if(springPropertiesDatas==null)
        {
            try {
                this.springPropertiesDatas=super.mergeProperties();
            } catch (IOException e) {
                log.equals(e);
            }
        }
        String value=this.springPropertiesDatas.get(key).toString();
        if(this.decodePropertyNames.contains(key))
        {
            value=MessageDigestCodingUtil.decode(value);
        }
        return value;
        
    }

}

 

  4.在applicationContext.xml文件添加如下設置,

<!-- 自定義的屬性解析文件 -->
    <bean id="appPropertyConfigurer"
        class="com.fjep.common.PropertyConfigurer"
        abstract="false" lazy-init="default" autowire="default">

        <property name="locations">
            <list>
                <value>classpath:/config/database.properties</value>
            </list>
        </property>
        <property name="decodePropertyNames">
           <list>
             <value>database.dev.username</value>
             <value>database.dev.password</value>
           </list>
        </property>
    </bean>

 

5.datasouce.property

#本地數據庫連接
database.dev.password=636d397664413d3d
database.dev.url=jdbc:mysql://localhost:3306/selected
database.dev.username=636d397664413d3d


database.driver=com.mysql.jdbc.Driver
hibernate.show_sql=false
#初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。
database.initialPoolSize=2
#連接池中保留的最小連接數
database.minPoolSize=2
#連接池中保留的最大連接數。
database.maxPoolSize=8
#最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0
database.maxIdleTime=60
#每30分鍾檢查所有連接池中的空閑連接。Default: 0
database.idleConnectionTestPeriod=1800
#org.gjt.mm.mysql.Driver
#com.mysql.jdbc.Driver
database.checkoutTimeout=60000
#org.gjt.mm.mysql.Driver
#com.mysql.jdbc.Driver

database.timeBetweenEvictionRunsMillis=60000  
database.minEvictableIdleTimeMillis=300000  

database.validationQuery=SELECT 'x' from dual 
database.testWhileIdle=true  
database.testOnBorrow=false  
database.testOnReturn=false 
database.filters=stat
database.poolPreparedStatements=true  
database.maxOpenPreparedStatements=50

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM