PropertiesUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * FileName:PropertiesUtil.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
  22. import java.io.IOException;
  23. import java.util.*;
  24. /**
  25. * hermit
  26. */
  27. public final class PropertiesUtil extends PropertyPlaceholderConfigurer {
  28. private static final byte[] KEY = {9, -1, 0, 5, 39, 8, 6, 19};
  29. private static Map<String, String> ctxPropertiesMap;
  30. private List<String> decryptProperties;
  31. @Override
  32. protected void loadProperties(Properties props)
  33. throws IOException {
  34. super.loadProperties(props);
  35. if (ctxPropertiesMap == null) {
  36. ctxPropertiesMap = new HashMap<String, String>();
  37. }
  38. for (Object key : props.keySet()) {
  39. String keyStr = key.toString();
  40. String value = props.getProperty(keyStr);
  41. if (decryptProperties != null && decryptProperties.contains(keyStr)) {
  42. value = SecurityUtil.decryptDes(value, KEY);
  43. props.setProperty(keyStr, value);
  44. }
  45. ctxPropertiesMap.put(keyStr, value);
  46. }
  47. }
  48. /**
  49. *
  50. * @param decryptProperties
  51. */
  52. public void setDecryptProperties(List<String> decryptProperties) {
  53. this.decryptProperties = decryptProperties;
  54. }
  55. /**
  56. * Get a value based on key , if key does not exist , null is returned
  57. *
  58. * @param key
  59. * @return
  60. */
  61. public static String getString(String key) {
  62. try {
  63. return ctxPropertiesMap.get(key);
  64. } catch (MissingResourceException e) {
  65. return null;
  66. }
  67. }
  68. /**
  69. * 根据key获取值
  70. *
  71. * @param key
  72. * @return
  73. */
  74. public static int getInt(String key) {
  75. return Integer.parseInt(ctxPropertiesMap.get(key));
  76. }
  77. /**
  78. * 根据key获取值
  79. *
  80. * @param key
  81. * @param defaultValue
  82. * @return
  83. */
  84. public static int getInt(String key, int defaultValue) {
  85. String value = ctxPropertiesMap.get(key);
  86. if (StringUtils.isBlank(value)) {
  87. return defaultValue;
  88. }
  89. return Integer.parseInt(value);
  90. }
  91. /**
  92. * 根据key获取值
  93. *
  94. * @param key
  95. * @param defaultValue
  96. * @return
  97. */
  98. public static boolean getBoolean(String key, boolean defaultValue) {
  99. String value = ctxPropertiesMap.get(key);
  100. if (StringUtils.isBlank(value)) {
  101. return defaultValue;
  102. }
  103. return new Boolean(value);
  104. }
  105. public static void main(String[] args) {
  106. String encrypt = SecurityUtil.encryptDes("buzhidao", KEY);
  107. System.out.println(encrypt);
  108. System.out.println(SecurityUtil.decryptDes(encrypt, KEY));
  109. }
  110. }