重写spring读取配置文件,读取加密配置文件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

[Java]代码    

package com.templet.spring;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	private Resource[] locations;

	public void setLocations(Resource[] locations) {
		this.locations = locations;
	}

	public void loadProperties(Properties props) throws IOException {
		if (this.locations != null) {
			PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
			for (int i = 0; i < this.locations.length; i++) {
				Resource location = this.locations[i];
				if (logger.isInfoEnabled()) {
					logger.info("Loading properties file from " + location);
				}
				InputStream is = null;
				try {
					is = location.getInputStream();
					BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
					StringBuffer buffer = new StringBuffer();
					String line = "";
					while ((line = in.readLine()) != null) {
						buffer.append(line);
					}
					String rs="";
					if("dbconn.properties".equals(location.getFilename())){
						DESUtil des = new DESUtil("1234567");
						rs=des.decryptStr(buffer.toString());
					}else{
						rs =buffer.toString();
					}
					propertiesPersister.load(props, new ByteArrayInputStream(rs.getBytes()));
				} finally {
					if (is != null) {
						is.close();
					}
				}
			}
		}
	}
}

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java实现DES加密解密代码

下一篇:iOS实现抽屉效果