@Test public void testInitSSLContext() throws Exception { payConfig.setMchId("123"); payConfig.setKeyPath("classpath:/abc.p12"); payConfig.initSSLContext(); }
public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getMchId())) { throw new WxPayException("请确保商户号mchId已设置"); } InputStream inputStream; if (this.keyContent != null) { inputStream = new ByteArrayInputStream(this.keyContent); } else { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } final String prefix = "classpath:"; String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题，请核实！"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在，请核实！"; if (this.getKeyPath().startsWith(prefix)) { String path = StringUtils.removeFirst(this.getKeyPath(), 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.getKeyPath()); if (!file.exists()) { throw new WxPayException(fileNotFoundMsg); } inputStream = new FileInputStream(file); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } } try { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题，请核实！", e); } finally { IOUtils.closeQuietly(inputStream); } }
WxPayConfig { public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getMchId())) { throw new WxPayException("请确保商户号mchId已设置"); } InputStream inputStream; if (this.keyContent != null) { inputStream = new ByteArrayInputStream(this.keyContent); } else { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } final String prefix = "classpath:"; String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题，请核实！"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在，请核实！"; if (this.getKeyPath().startsWith(prefix)) { String path = StringUtils.removeFirst(this.getKeyPath(), 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.getKeyPath()); if (!file.exists()) { throw new WxPayException(fileNotFoundMsg); } inputStream = new FileInputStream(file); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } } try { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题，请核实！", e); } finally { IOUtils.closeQuietly(inputStream); } } }
WxPayConfig { public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getMchId())) { throw new WxPayException("请确保商户号mchId已设置"); } InputStream inputStream; if (this.keyContent != null) { inputStream = new ByteArrayInputStream(this.keyContent); } else { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } final String prefix = "classpath:"; String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题，请核实！"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在，请核实！"; if (this.getKeyPath().startsWith(prefix)) { String path = StringUtils.removeFirst(this.getKeyPath(), 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.getKeyPath()); if (!file.exists()) { throw new WxPayException(fileNotFoundMsg); } inputStream = new FileInputStream(file); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } } try { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题，请核实！", e); } finally { IOUtils.closeQuietly(inputStream); } }  }
WxPayConfig { public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getMchId())) { throw new WxPayException("请确保商户号mchId已设置"); } InputStream inputStream; if (this.keyContent != null) { inputStream = new ByteArrayInputStream(this.keyContent); } else { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } final String prefix = "classpath:"; String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题，请核实！"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在，请核实！"; if (this.getKeyPath().startsWith(prefix)) { String path = StringUtils.removeFirst(this.getKeyPath(), 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.getKeyPath()); if (!file.exists()) { throw new WxPayException(fileNotFoundMsg); } inputStream = new FileInputStream(file); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } } try { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题，请核实！", e); } finally { IOUtils.closeQuietly(inputStream); } }  SSLContext initSSLContext(); }
WxPayConfig { public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getMchId())) { throw new WxPayException("请确保商户号mchId已设置"); } InputStream inputStream; if (this.keyContent != null) { inputStream = new ByteArrayInputStream(this.keyContent); } else { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } final String prefix = "classpath:"; String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题，请核实！"; String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在，请核实！"; if (this.getKeyPath().startsWith(prefix)) { String path = StringUtils.removeFirst(this.getKeyPath(), 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.getKeyPath()); if (!file.exists()) { throw new WxPayException(fileNotFoundMsg); } inputStream = new FileInputStream(file); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } } try { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题，请核实！", e); } finally { IOUtils.closeQuietly(inputStream); } }  SSLContext initSSLContext();  }