给客户主机部署项目的时候, 难免客户机上部署的有其他项目, 所以这个配置文件加密还是有必要的, 分享一个PropKit功能增强的小工具~ 有不妥的地方,麻烦帮忙请指出来下~
使用方法:
PropDesKit.use("a_little_config.txt");
String jdbcUrl = PropKit.get("jdbcUrl");
System.out.println(jdbcUrl);上线的时候,删掉 WEB-INF\classes\下的配置文件如:a_little_config.txt
就搞定了~
代码:
package com.demo.text.tool;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import com.jfinal.core.Const;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
/** PropKit功能增强:DES加密
* @author dufuzhong@126.com
* @date 2017年3月26日 下午10:22:42
*
*/
public class PropDesKit {
public static void main(String[] args) {
PropDesKit.use("a_little_config.txt");
String jdbcUrl = PropKit.get("jdbcUrl");
System.out.println(jdbcUrl);
}
private PropDesKit() {}
/**
* Using the properties file. It will loading the properties file if not loading.
* @param strKey
* @see #use(String, String)
*/
public static Prop use(String fileName, String strKey) {
return use(fileName, Const.DEFAULT_ENCODING, strKey);
}
/**
* Using the properties file. It will loading the properties file if not loading.
* @param strKey
* @see #use(String, String)
*/
public static Prop use(String fileName) {
return use(fileName, Const.DEFAULT_ENCODING, "JfinalDaFaHao2333");// 改成自己的 密码, 方便使用
}
/**
* Using the properties file. It will loading the properties file if not loading.
* <p>
* Example:<br>
* PropKit.use("config.txt", "UTF-8");<br>
* PropKit.use("other_config.txt", "UTF-8");<br><br>
* String userName = PropKit.get("userName");<br>
* String password = PropKit.get("password");<br><br>
*
* userName = PropKit.use("other_config.txt").get("userName");<br>
* password = PropKit.use("other_config.txt").get("password");<br><br>
*
* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
*
* @param fileName the properties file's name in classpath or the sub directory of classpath
* @param encoding the encoding
* @param strKey
*/
public static Prop use(String fileName, String encoding, String strKey) {
Prop prop = null;
InputStream inputStream = null;
try {
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
File destFile = new File(PathKit.getRootClassPath() + "/" + fileName + "pro");
if (inputStream != null) {
prop = PropKit.use(fileName, encoding);
// 加密
encrypt(inputStream, destFile, strKey);
}else{
inputStream = new FileInputStream(destFile);
// 临时文件
File dest = new File(PathKit.getRootClassPath() + "/" + fileName);
// 解密
decrypt(inputStream, dest, strKey);
prop = PropKit.use(fileName, encoding);
dest.delete();
}
} catch (IOException e) {
throw new RuntimeException("Error loading properties file.", e);
} catch (Exception e) {
throw new RuntimeException("Error encrypt properties file.", e);
}
finally {
if (inputStream != null) try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
}
return prop;
}
/**
* 根据参数生成KEY
*/
private static Key getKey(String strKey) {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
Key key = generator.generateKey();
generator = null;
return key;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}
/**
* 进行加密并保存目标文件destFile中
*/
private static void encrypt( InputStream is, File destFile, String strKey) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey(strKey));
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
out.close();
}
/**
* 采用DES算法解密
*/
private static void decrypt(InputStream is, File dest, String strKey) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey(strKey));
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
}
}2333 忘记打广告了....
ps: 有需求的可以联系~
摩码创想(北京)科技有限公司 赵 睿 ZhaoRui
Tel:186 0067 6071 QQ:28747355 微信:zhaorui93
--------------------------------------------------------------------------
“教育IT化”一站式全套解决方案 {1v1量身设计,满意付费 } 免费升级,终身维护!
学员档案 CRM、市场招生、渠道管理、销售管理、报名交费、排课教务、教学评价、课件管理、通知提醒、同步课表、课酬考勤、成绩管理、财务管理、数据统计、宿舍管理、库存教材、人事行政、学习报告、就业管理、在线支付、网上预约、在线购课、在线考试、网络课堂(移动端、微信端、数据批量导入、导出,打印、会员卡、积分) + 更多自由设计功能