|
@@ -15,78 +15,79 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
|
@@ -15,78 +15,79 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
15
|
import org.apache.http.ssl.SSLContextBuilder;
|
15
|
import org.apache.http.ssl.SSLContextBuilder;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
|
17
|
|
18
|
-import javax.annotation.PostConstruct;
|
|
|
19
|
import javax.net.ssl.HostnameVerifier;
|
18
|
import javax.net.ssl.HostnameVerifier;
|
20
|
import javax.net.ssl.SSLContext;
|
19
|
import javax.net.ssl.SSLContext;
|
21
|
|
20
|
|
22
|
public abstract class HttpSslClientAbstract extends HttpClient {
|
21
|
public abstract class HttpSslClientAbstract extends HttpClient {
|
23
|
|
22
|
|
24
|
- //最大总数
|
|
|
25
|
- private final int sslMaxTotal = 10;
|
|
|
26
|
-
|
|
|
27
|
- //默认并发数
|
|
|
28
|
- private final int sslDefaultMaxPerRoute = 5;
|
|
|
29
|
-
|
|
|
30
|
- @Autowired
|
|
|
31
|
- private WechatHelper wechatHelper;
|
|
|
32
|
-
|
|
|
33
|
- protected abstract String getMchId();
|
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
- @Override
|
|
|
37
|
- public CloseableHttpClient buildHttpClient() throws Exception {
|
|
|
38
|
- HttpClientBuilder b = HttpClientBuilder.create();
|
|
|
39
|
-
|
|
|
40
|
- // setup a Trust Strategy that allows all certificates.
|
|
|
41
|
- SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
|
|
|
42
|
- sslContextBuilder.loadTrustMaterial(null, (arg0, arg1) -> true);
|
|
|
43
|
- loadKeyMaterial(sslContextBuilder);
|
|
|
44
|
- SSLContext sslContext = sslContextBuilder.build();
|
|
|
45
|
- b.setSSLContext(sslContext);
|
|
|
46
|
-
|
|
|
47
|
- // don't check Hostnames, either.
|
|
|
48
|
- // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
|
|
|
49
|
- // you don't want to weaken
|
|
|
50
|
- HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
|
|
|
51
|
-
|
|
|
52
|
- // here's the special part:
|
|
|
53
|
- // -- need to create an SSL Socket Factory, to use our weakened
|
|
|
54
|
- // "trust strategy";
|
|
|
55
|
- // -- and create a Registry, to register it.
|
|
|
56
|
- //
|
|
|
57
|
- SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
|
|
|
58
|
- sslContext, hostnameVerifier);
|
|
|
59
|
- Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
|
|
|
60
|
- .<ConnectionSocketFactory> create()
|
|
|
61
|
- .register("http",
|
|
|
62
|
- PlainConnectionSocketFactory.getSocketFactory())
|
|
|
63
|
- .register("https", sslSocketFactory).build();
|
|
|
64
|
-
|
|
|
65
|
- // now, we create connection-manager using our Registry.
|
|
|
66
|
- // -- allows multi-threaded use
|
|
|
67
|
- PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
|
|
|
68
|
- socketFactoryRegistry);
|
|
|
69
|
- connMgr.setMaxTotal(sslMaxTotal);
|
|
|
70
|
- connMgr.setDefaultMaxPerRoute(sslDefaultMaxPerRoute);
|
|
|
71
|
- b.setConnectionManager(connMgr);
|
23
|
+ //最大总数
|
|
|
24
|
+ private final int sslMaxTotal = 10;
|
|
|
25
|
+
|
|
|
26
|
+ //默认并发数
|
|
|
27
|
+ private final int sslDefaultMaxPerRoute = 5;
|
|
|
28
|
+
|
|
|
29
|
+ @Autowired
|
|
|
30
|
+ private WechatHelper wechatHelper;
|
|
|
31
|
+
|
|
|
32
|
+ protected abstract String getMchId();
|
|
|
33
|
+
|
|
|
34
|
+ protected abstract String getMchCertPath();
|
|
|
35
|
+
|
|
|
36
|
+
|
|
|
37
|
+ @Override
|
|
|
38
|
+ public CloseableHttpClient buildHttpClient() throws Exception {
|
|
|
39
|
+ HttpClientBuilder b = HttpClientBuilder.create();
|
|
|
40
|
+
|
|
|
41
|
+ // setup a Trust Strategy that allows all certificates.
|
|
|
42
|
+ SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
|
|
|
43
|
+ sslContextBuilder.loadTrustMaterial(null, (arg0, arg1) -> true);
|
|
|
44
|
+ loadKeyMaterial(sslContextBuilder);
|
|
|
45
|
+ SSLContext sslContext = sslContextBuilder.build();
|
|
|
46
|
+ b.setSSLContext(sslContext);
|
|
|
47
|
+
|
|
|
48
|
+ // don't check Hostnames, either.
|
|
|
49
|
+ // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
|
|
|
50
|
+ // you don't want to weaken
|
|
|
51
|
+ HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
|
|
|
52
|
+
|
|
|
53
|
+ // here's the special part:
|
|
|
54
|
+ // -- need to create an SSL Socket Factory, to use our weakened
|
|
|
55
|
+ // "trust strategy";
|
|
|
56
|
+ // -- and create a Registry, to register it.
|
|
|
57
|
+ //
|
|
|
58
|
+ SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
|
|
|
59
|
+ sslContext, hostnameVerifier);
|
|
|
60
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
|
|
|
61
|
+ .<ConnectionSocketFactory>create()
|
|
|
62
|
+ .register("http",
|
|
|
63
|
+ PlainConnectionSocketFactory.getSocketFactory())
|
|
|
64
|
+ .register("https", sslSocketFactory).build();
|
|
|
65
|
+
|
|
|
66
|
+ // now, we create connection-manager using our Registry.
|
|
|
67
|
+ // -- allows multi-threaded use
|
|
|
68
|
+ PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
|
|
|
69
|
+ socketFactoryRegistry);
|
|
|
70
|
+ connMgr.setMaxTotal(sslMaxTotal);
|
|
|
71
|
+ connMgr.setDefaultMaxPerRoute(sslDefaultMaxPerRoute);
|
|
|
72
|
+ b.setConnectionManager(connMgr);
|
72
|
// BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(socketFactoryRegistry);
|
73
|
// BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(socketFactoryRegistry);
|
73
|
// b.setConnectionManager(connMgr);
|
74
|
// b.setConnectionManager(connMgr);
|
74
|
-
|
|
|
75
|
- //request config
|
|
|
76
|
- RequestConfig requestConfig = RequestConfig.custom()
|
|
|
77
|
- .setConnectionRequestTimeout(connectionRequestTimeout)
|
|
|
78
|
- .setConnectTimeout(connectionTimeout)
|
|
|
79
|
- .setSocketTimeout(socketTimeout)
|
|
|
80
|
- .build();
|
|
|
81
|
- b.setDefaultRequestConfig(requestConfig);
|
|
|
82
|
-
|
|
|
83
|
- // finally, buildSellerBo the HttpClient;
|
|
|
84
|
- // -- done!
|
|
|
85
|
- return b.build();
|
|
|
86
|
- }
|
|
|
87
|
-
|
|
|
88
|
- private void loadKeyMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
|
|
|
89
|
- wechatHelper.loadKeyMaterial(getMchId(), sslContextBuilder);
|
|
|
90
|
- }
|
|
|
91
|
-
|
75
|
+
|
|
|
76
|
+ //request config
|
|
|
77
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
78
|
+ .setConnectionRequestTimeout(connectionRequestTimeout)
|
|
|
79
|
+ .setConnectTimeout(connectionTimeout)
|
|
|
80
|
+ .setSocketTimeout(socketTimeout)
|
|
|
81
|
+ .build();
|
|
|
82
|
+ b.setDefaultRequestConfig(requestConfig);
|
|
|
83
|
+
|
|
|
84
|
+ // finally, buildSellerBo the HttpClient;
|
|
|
85
|
+ // -- done!
|
|
|
86
|
+ return b.build();
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ private void loadKeyMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
|
|
|
90
|
+ wechatHelper.loadKeyMaterial(getMchId(), getMchCertPath(), sslContextBuilder);
|
|
|
91
|
+ }
|
|
|
92
|
+
|
92
|
} |
93
|
} |