mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-04 06:07:59 +08:00 
			
		
		
		
	微信支付证书路径判断代码优化
This commit is contained in:
		@ -158,18 +158,27 @@ public class WxPayConfig {
 | 
			
		||||
 | 
			
		||||
    InputStream inputStream;
 | 
			
		||||
    final String prefix = "classpath:";
 | 
			
		||||
    String fileHasProblemMsg = "证书文件【" + this.keyPath + "】有问题,请核实!";
 | 
			
		||||
    String fileNotFoundMsg = "证书文件【" + this.keyPath + "】不存在,请核实!";
 | 
			
		||||
    if (this.keyPath.startsWith(prefix)) {
 | 
			
		||||
      inputStream = WxPayConfig.class.getResourceAsStream(this.keyPath.replace(prefix, ""));
 | 
			
		||||
      String path = StringUtils.removeFirst(this.keyPath, prefix);
 | 
			
		||||
      if (!path.startsWith("/")) {
 | 
			
		||||
        path = "/" + path;
 | 
			
		||||
      }
 | 
			
		||||
      inputStream = WxPayConfig.class.getResourceAsStream(path);
 | 
			
		||||
      if (inputStream == null) {
 | 
			
		||||
        throw new WxPayException(fileNotFoundMsg);
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      try {
 | 
			
		||||
        File file = new File(this.keyPath);
 | 
			
		||||
        if (!file.exists()) {
 | 
			
		||||
          throw new WxPayException("证书文件【" + file.getPath() + "】不存在!");
 | 
			
		||||
          throw new WxPayException(fileNotFoundMsg);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        inputStream = new FileInputStream(file);
 | 
			
		||||
      } catch (IOException e) {
 | 
			
		||||
        throw new WxPayException("证书文件有问题,请核实!", e);
 | 
			
		||||
        throw new WxPayException(fileHasProblemMsg, e);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -180,7 +189,7 @@ public class WxPayConfig {
 | 
			
		||||
      this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
 | 
			
		||||
      return this.sslContext;
 | 
			
		||||
    } catch (Exception e) {
 | 
			
		||||
      throw new WxPayException("证书文件有问题,请核实!", e);
 | 
			
		||||
      throw new WxPayException(fileHasProblemMsg, e);
 | 
			
		||||
    } finally {
 | 
			
		||||
      IOUtils.closeQuietly(inputStream);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user