RSAKeys.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * FileName:RSAKeys.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.security;
  20. import com.daju.base.exception.BusinessException;
  21. import org.apache.log4j.Logger;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.security.PublicKey;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.Properties;
  29. /**
  30. * RSA秘钥文件缓存类
  31. * @author xiaosf
  32. * @date 2017年6月13日
  33. */
  34. public class RSAKeys {
  35. /**
  36. * 日志对象
  37. */
  38. private static Logger logger=Logger.getLogger(RSAKeys.class);
  39. /**
  40. * 公钥
  41. */
  42. private static Map<String,PublicKey> pubKey=new HashMap<String,PublicKey>();
  43. //初始化公钥
  44. static{
  45. fresh();
  46. }
  47. /**
  48. * 根据系统id获取公钥
  49. * @param sysId 系统id
  50. * @return 公钥
  51. * @throws Exception 找不到对应的公钥抛出此异常
  52. */
  53. public static PublicKey getPubKeyBySys(String sysId) throws Exception{
  54. PublicKey key=pubKey.get(sysId);
  55. if(key==null){
  56. throw new Exception("系统["+sysId+"]没有对应的公钥配置");
  57. }
  58. return key;
  59. }
  60. /**
  61. * 刷新
  62. */
  63. private static void fresh(){
  64. InputStream is=RSAKeys.class.getClassLoader().getResourceAsStream("property/keys.properties");
  65. try {
  66. Properties prop=new Properties();
  67. prop.load(is);
  68. is.close();
  69. String keyFile=null;
  70. String[] key=null;
  71. RSASignatureTools.KeyStoreFileType keyFileType= RSASignatureTools.KeyStoreFileType.BINARY;
  72. for(Entry<Object, Object> entry:prop.entrySet()){
  73. if(pubKey.get(entry.getKey())!=null){
  74. continue;
  75. }
  76. keyFile=(String)entry.getValue();
  77. key=keyFile.split(":::");
  78. if(key.length>1&&"base64".equalsIgnoreCase(key[1])){
  79. keyFileType= RSASignatureTools.KeyStoreFileType.BASE64;
  80. }
  81. pubKey.put((String)entry.getKey(), RSASignatureTools.generateRSAPubKey(key[0], keyFileType));
  82. }
  83. } catch (Exception e) {
  84. logger.error(e.getMessage(), e);
  85. }finally{
  86. try {
  87. is.close();
  88. } catch (IOException e) {
  89. logger.error("解析秘钥配置文件错误,请确认classpath是否有文件property/keys.properties并且数据格式如key=file:type", e);
  90. }
  91. }
  92. }
  93. /**
  94. * 添加新的公钥,如果已经存在则更新
  95. * @param sysid 系统标识
  96. * @param keyFile 秘钥文件
  97. * @param keyType 秘钥存储类型
  98. * @throws BusinessException
  99. */
  100. public static void newKey(String sysid,String keyFile, RSASignatureTools.KeyStoreFileType keyType) throws BusinessException{
  101. try {
  102. pubKey.put(sysid, RSASignatureTools.generateRSAPubKey(keyFile, keyType));
  103. } catch (Exception e) {
  104. throw new BusinessException("添加公钥失败:"+sysid+"-"+keyFile+"-"+keyType,e);
  105. }
  106. }
  107. }