/*
* FileName:PropertiesUtil.java
*
* Copyright (c) 2017-2020, hermit (794890569@qq.com).
*
* Licensed under the GNU General Public License, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cn.com.lzt.common.util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import java.io.IOException;
import java.util.*;
/**
* hermit
*/
public final class PropertiesUtil extends PropertyPlaceholderConfigurer {
private static final byte[] KEY = {9, -1, 0, 5, 39, 8, 6, 19};
private static Map ctxPropertiesMap;
private List decryptProperties;
@Override
protected void loadProperties(Properties props)
throws IOException {
super.loadProperties(props);
if (ctxPropertiesMap == null) {
ctxPropertiesMap = new HashMap();
}
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
if (decryptProperties != null && decryptProperties.contains(keyStr)) {
value = SecurityUtil.decryptDes(value, KEY);
props.setProperty(keyStr, value);
}
ctxPropertiesMap.put(keyStr, value);
}
}
/**
*
* @param decryptProperties
*/
public void setDecryptProperties(List decryptProperties) {
this.decryptProperties = decryptProperties;
}
/**
* Get a value based on key , if key does not exist , null is returned
*
* @param key
* @return
*/
public static String getString(String key) {
try {
return ctxPropertiesMap.get(key);
} catch (MissingResourceException e) {
return null;
}
}
/**
* 根据key获取值
*
* @param key
* @return
*/
public static int getInt(String key) {
return Integer.parseInt(ctxPropertiesMap.get(key));
}
/**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static int getInt(String key, int defaultValue) {
String value = ctxPropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return Integer.parseInt(value);
}
/**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = ctxPropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return new Boolean(value);
}
public static void main(String[] args) {
String encrypt = SecurityUtil.encryptDes("buzhidao", KEY);
System.out.println(encrypt);
System.out.println(SecurityUtil.decryptDes(encrypt, KEY));
}
}