| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * FileName:PropertiesUtil.java
- * <p>
- * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
- * <p>
- * 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
- * <p>
- * http://www.gnu.org/licenses/gpl-3.0.html
- * <p>
- * 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<String, String> ctxPropertiesMap;
- private List<String> decryptProperties;
- @Override
- protected void loadProperties(Properties props)
- throws IOException {
- super.loadProperties(props);
- if (ctxPropertiesMap == null) {
- ctxPropertiesMap = new HashMap<String, String>();
- }
- 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<String> 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));
- }
- }
|