Authored by mali

Merge branch 'test6.9.13' of http://git.yoho.cn/ufo/yohoufo-fore into test6.9.13

... ... @@ -55,21 +55,8 @@ public class HttpClient {
private CloseableHttpClient httpClient;
@PostConstruct
public void init() {
new Thread(() -> {
while (Objects.isNull(httpClient)) {
try {
httpClient = this.buildHttpClient();
} catch (Exception e) {
log.warn("build http client fail, {}", e.getMessage());
}
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
}
}
}, "http-client-builder-" + this.getClass().getSimpleName()).start();
public void init() throws Exception {
httpClient = this.buildHttpClient();
}
public CloseableHttpClient buildHttpClient() throws Exception {
... ...
... ... @@ -15,78 +15,79 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
public abstract class HttpSslClientAbstract extends HttpClient {
//最大总数
private final int sslMaxTotal = 10;
//默认并发数
private final int sslDefaultMaxPerRoute = 5;
@Autowired
private WechatHelper wechatHelper;
protected abstract String getMchId();
@Override
public CloseableHttpClient buildHttpClient() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, (arg0, arg1) -> true);
loadKeyMaterial(sslContextBuilder);
SSLContext sslContext = sslContextBuilder.build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
// you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened
// "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http",
PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
connMgr.setMaxTotal(sslMaxTotal);
connMgr.setDefaultMaxPerRoute(sslDefaultMaxPerRoute);
b.setConnectionManager(connMgr);
//最大总数
private final int sslMaxTotal = 10;
//默认并发数
private final int sslDefaultMaxPerRoute = 5;
@Autowired
private WechatHelper wechatHelper;
protected abstract String getMchId();
protected abstract String getMchCertPath();
@Override
public CloseableHttpClient buildHttpClient() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, (arg0, arg1) -> true);
loadKeyMaterial(sslContextBuilder);
SSLContext sslContext = sslContextBuilder.build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
// you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened
// "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("http",
PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
connMgr.setMaxTotal(sslMaxTotal);
connMgr.setDefaultMaxPerRoute(sslDefaultMaxPerRoute);
b.setConnectionManager(connMgr);
// BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(socketFactoryRegistry);
// b.setConnectionManager(connMgr);
//request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.build();
b.setDefaultRequestConfig(requestConfig);
// finally, buildSellerBo the HttpClient;
// -- done!
return b.build();
}
private void loadKeyMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
wechatHelper.loadKeyMaterial(getMchId(), sslContextBuilder);
}
//request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.build();
b.setDefaultRequestConfig(requestConfig);
// finally, buildSellerBo the HttpClient;
// -- done!
return b.build();
}
private void loadKeyMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
wechatHelper.loadKeyMaterial(getMchId(), getMchCertPath(), sslContextBuilder);
}
}
... ...
... ... @@ -12,4 +12,10 @@ public class WxMiniappHttpSslClient extends HttpSslClientAbstract {
protected String getMchId() {
return WeixinPayConfig.Miniapp.MALL_ID;
}
@Override
protected String getMchCertPath() {
return WeixinPayConfig.Miniapp.APP_PARTNER_CERT;
}
}
... ...
... ... @@ -11,4 +11,9 @@ public class WxUFORealAppHttpSslClient extends HttpSslClientAbstract {
return WeixinPayConfig.PARTNER_ID;
}
@Override
protected String getMchCertPath() {
return WeixinPayConfig.WECHAT_PAY_UFOREAL_APP_PARTNER_CERT;
}
}
... ...