properties文件加密处理方法

1.在application.xml文件中加载config.properties文件方式(会将config.properties的配置加载进来)
<bean class="com.paic.utils.EncryptPropertyConfigurer这个工具类
">
  <property name = "location" value="classpath:config.properties的配置加载进来"></property>
  <property name = "fileEncoding" value="UTF-8"></property>
</bean>
2.加密方法
/**
 * 密码工具类
 * Created by Li.fang on 2014/11/13.
 */
public class CipherUtils {
    private static Logger logger = LoggerFactory.getLogger(CipherUtils.class);
    //指定DES加密解密所用密钥
    private static Key key;
    private static String STR_SALT = "APLUS OFFICE SYSTEM";
    private static final String GENERATOR_DES = "DES";
    private static final String ENCODING_UTF8 = "UTF-8";
    private static final int CIPHER_COUNT = 5;
    static {
        try {
            KeyGenerator generator = KeyGenerator.getInstance(GENERATOR_DES);
            generator.init(new SecureRandom(STR_SALT.getBytes()));
            key = generator.generateKey();
            generator = null;
        }catch (NoSuchAlgorithmException e){
            logger.warn("Cryptographic failure");
            throw new RuntimeException(e);
        }
    }

    /**
     * 对字符串进行DES加密,返回BASE64编码的加密字符串
     * @param str 需要加密的字符串
     * @return 加密后的字符串
     */
    public static String getEncryptBase64String(String str){
//        BASE64Encoder base64Encoder = new BASE64Encoder();
        try{
            byte[] strBytes = str.getBytes(ENCODING_UTF8);
            Cipher cipher= Cipher.getInstance(GENERATOR_DES);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return Base64.encodeBase64String(encryptStrBytes);
        }catch (Exception e){
            logger.warn("Cryptographic failure");
            throw new RuntimeException(e);
        }
    }
    /**
     * 对BASE64编码的加密字符串进行解密,返回解密后的字符串
     * @param str BASE64加密字符串
     * @return 解密后的字符串
     */
    public static String getDecryptString(String str){
//        BASE64Decoder base64Decoder = new BASE64Decoder();
        try{
            byte[] strBytes = Base64.decodeBase64(str);
            Cipher cipher= Cipher.getInstance(GENERATOR_DES);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decryptStrBytes = cipher.doFinal(strBytes);
            return new String(decryptStrBytes, ENCODING_UTF8);
        }catch (Exception e){
            logger.warn("Decryption failure");
            throw new RuntimeException(e);
        }
    }
}
3.写EncryptPropertyConfigurer这个工具类
package com.aplus.method;
import com.aplus.utils.CipherUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
 * 继承PropertyPlaceholderConfigurer,配置支持密文属性的属性文件
 * Created by li.fang on 2014/11/13.
 */
public class EncryptPropertyConfigurer extends PropertyPlaceholderConfigurer{
    private Logger logger = LoggerFactory.getLogger(EncryptPropertyConfigurer.class);
    //需要解密的属性
    private String[] encryptPropNames = {"jdbc.url","jdbc.username","jdbc.password"};
    /**
     * 对特定属性的属性值进行转换
     * @param propertyName 属性名称
     * @param propertyValue 属性值
     * @return
     */
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        logger.info("The properties name is : {}, value is : ", propertyName, propertyValue);
        if(isEncryptProp(propertyName)){
            String decryptValue = CipherUtils.getDecryptString(propertyValue);
            logger.info(decryptValue);
            return decryptValue;
        }else{
            return propertyValue;
        }
    }
    /**
     * 判断属性是否需要解密
     * @param propertyName
     * @return
     */
    private boolean isEncryptProp(String propertyName){
        for(String encryptPropName : encryptPropNames){
            if(StringUtils.equals(encryptPropName,propertyName)){
                return true;
            }
        }
        return false;
    }
}
使用URL下载到指定的文件中:

使用URL下载指定的文件保存到指定的文件夹中。
/*
 * 使用URL下载指定的文件保存到指定的文件夹中。
 * 类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。
 * 资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,
 * 例如对数据库或搜索引擎的查询。
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {
public static void main(String[] args) {
try {
DownLoadUtil.download("指定的网站", "保存的文件名", "保存到指定地方");
} catch (IOException e) {
e.printStackTrace();
}
}
}
class DownLoadUtil{
public static void download
(String urlString,String fileName,String savePath)throws IOException {
URL url=new URL(urlString);
/* URLConnection conn=url.openConnection();
InputStream is=conn.getInputStream();*/
//使用一条指令代替上面的两条指令
InputStream is=url.openStream();
byte[] buff=new byte[1024];
int len=0;
//读操作
File file=new File(savePath);
if (!file.exists()) {
file.mkdirs();
}
//写操作
OutputStream os=new FileOutputStream(file.getAbsolutePath()+"\\"+fileName);
//一边读一边写
while ((len=is.read(buff))!=-1) {
os.write(buff, 0, len);//把读到写到指定的数组里面
//释放资源
os.close();
is.close();
}
}
}

猜你喜欢

转载自blog.csdn.net/tomcatAndOracle/article/details/80278648