spring中的数据源配置信息加密方案

关键字:spring中的数据源配置信息加密方案

附件是完整的实现及说明

在使用spring框架开发一些核心的系统时经常会收到客户提出重要信息加密的要求,在这里我就记录一下我实现Spring数据源配置信息JDBC属性的加密过程。
工具/原料

    JAVA开发环境
    Spring+mybatis+struts2

方法/步骤

    先在项目中新建一个加密算法类,代码如下:

    package com.cttsp.frame.util.encrypt;

    import java.security.Key;

    import java.security.SecureRandom;

    import javax.crypto.Cipher;

    import javax.crypto.KeyGenerator;

    import sun.misc.BASE64Decoder;

    import sun.misc.BASE64Encoder;

    /**

     * DES加密算法工具类

     * @author RyanCai

     * @date 2014-10-8 下午3:24:14

     */

    public class DESUtils {

    private static Key key;

        private static String KEY_STR = "myKey";// 密钥

        private static String CHARSETNAME = "UTF-8";// 编码

        private static String ALGORITHM = "DES";// 加密类型

    

        static {

            try {

                KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);

                generator.init(new SecureRandom(KEY_STR.getBytes()));

                key = generator.generateKey();

                generator = null;

            } catch (Exception e) {

                throw new RuntimeException(e);

            }

        }

    

        /**

         * 对str进行DES加密

         *

         * @param str

         * @return

         */

        public static String getEncryptString(String str) {

            BASE64Encoder base64encoder = new BASE64Encoder();

            try {

                byte[] bytes = str.getBytes(CHARSETNAME);

                Cipher cipher = Cipher.getInstance(ALGORITHM);

                cipher.init(Cipher.ENCRYPT_MODE, key);

                byte[] doFinal = cipher.doFinal(bytes);

                return base64encoder.encode(doFinal);

            } catch (Exception e) {

                throw new RuntimeException(e);

            }

        }

    

        /**

         * 对str进行DES解密

         *

         * @param str

         * @return

         */

        public static String getDecryptString(String str) {

            BASE64Decoder base64decoder = new BASE64Decoder();

            try {

                byte[] bytes = base64decoder.decodeBuffer(str);

                Cipher cipher = Cipher.getInstance(ALGORITHM);

                cipher.init(Cipher.DECRYPT_MODE, key);

                byte[] doFinal = cipher.doFinal(bytes);

                return new String(doFinal, CHARSETNAME);

            } catch (Exception e) {

                throw new RuntimeException(e);

            }

        }

    }

    继承实现一个加密配置属性处理类,代码如下:

    package com.cttsp.frame.util.encrypt;

    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

    /**

     * 继承PropertyPlaceholderConfigurer支持密文属性的属性配置器

     * @author <姓名>

     * @date 2014-10-8 下午3:34:14

     */

    public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private String[] encryptPropNames = { "username", "password" };

    

        @Override

        protected String convertProperty(String propertyName, String propertyValue) {

            if (isEncryptProp(propertyName)) {

                String decryptValue = DESUtils.getDecryptString(propertyValue);

                return decryptValue;

            } else {

                return propertyValue;

            }

        }

    

        /**

         * 判断是否是加密的属性

         *

         * @param propertyName

         * @return

         */

        private boolean isEncryptProp(String propertyName) {

            for (String encryptpropertyName : encryptPropNames) {

                if (encryptpropertyName.equals(propertyName))

                    return true;

            }

            return false;

        }

    }

    修改自己的jdbc属性文件,将需要加密的信息改成加密后的密文,我的配置文件如下图:
    对spring中的数据源配置信息加密

    再修改srping的配置文件,主要修改如下截图:
    对spring中的数据源配置信息加密
    5

    到这里所有的修改基本上完成了,启动项目正常连接数据库。


附件是完整的实现及说明

猜你喜欢

转载自zhaoshijie.iteye.com/blog/2146344