Spring的一种配置文件加密解密

最近公司查的严格 要求代码方面安全问题很严格,我呢,就一个人整了整代码出现的一下账号密码 ,在网上找了一种实现加密解密的方式。
话不多说,说原理:
很简单:读取配置文件的时候 将密码 解密了 然后 下面这些 拿到的是解密之后的 而配置文件properties中是加密的东西

username=QnbEdnemerw=
password=QnbEdnemerw=
    <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />

实现类 就俩 简单:
1.实现字符串加密解密类

import java.security.Key;  
import java.security.SecureRandom;  

import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  

import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder;  

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) {  
                // TODO: handle exception  
                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) {  
                // TODO: handle exception  
                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) {  
                // TODO: handle exception  
                throw new RuntimeException(e);  
            }  
        }  


        public static void main(String[] args) {//av21WkQRY3o=
            String encryptString = getEncryptString("slave");
            System.out.println(encryptString);

            String decryptString = getDecryptString("QnbEdnemerw=");
            System.out.println(decryptString);
        }
}

2.Spring内部的类 ,在项目启动是加载配置文件中的内容 ,我们继承此类 重写读取配置文件的方法

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

/** 
 * 继承PropertyPlaceholderConfigurer定义支持密文版属性的属性配置器 
 *  
 * @author moziqi 
 *  
 */  
public class EncryptPropertyPlaceholder extends  
        PropertyPlaceholderConfigurer {  
        //配置文件中要解密的键值对的key
    private String[] encryptPropNames = { "username", "password" };  

    @Override  
    protected String convertProperty(String propertyName, String propertyValue) {  
        if (isEncryptProp(propertyName)) {  
            String decryptValue = DESUtils.getDecryptString(propertyValue);  
            System.out.println(propertyName + "解密内容:" + decryptValue);  
            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;  
    }  


}  

最重要的一步:
application.xml中 以前加载配置文件的xml是这样的:

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

现在 注释掉上面的

加上这个:

    //class为自己写的那个类
 <bean class="com.ws.util.EncryptPropertyPlaceholder"  
        p:location="classpath:w_s_dzqd.properties" p:fileEncoding="utf-8" /> 

很简单

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值