Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
asset-distribution
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
data-spider
asset-distribution
Commits
352eb876
Commit
352eb876
authored
Apr 01, 2020
by
郑建
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加删除临时文件
parent
87f50b15
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
84 deletions
+58
-84
HttpClientConfig.java
...roup/asset/distribution/config/http/HttpClientConfig.java
+58
-84
No files found.
src/main/java/com/quantgroup/asset/distribution/config/http/HttpClientConfig.java
View file @
352eb876
...
@@ -105,96 +105,70 @@ public class HttpClientConfig {
...
@@ -105,96 +105,70 @@ public class HttpClientConfig {
}
}
@Bean
(
"niwodaiHttpsClient"
)
@Bean
(
"niwodaiHttpsClient"
)
public
CloseableHttpClient
niwodaiHttpsClient
()
throws
Exception
{
public
CloseableHttpClient
niwodaiHttpsClient
()
throws
Exception
{
File
temp1
=
null
,
temp2
=
null
;
/**
try
{
* 创建TrustManager
/**
*/
* 创建TrustManager
X509TrustManager
xtm
=
new
X509TrustManager
()
{
*/
@Override
X509TrustManager
xtm
=
new
X509TrustManager
()
{
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
@Override
}
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
}
@Override
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
}
}
@Override
@Override
public
X509Certificate
[]
getAcceptedIssuers
()
{
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
null
;
return
null
;
}
}
};
};
String
classesPath
=
"/niwodai"
;
String
classesPath
=
"/niwodai"
;
KeyStore
keyStore
=
KeyStore
.
getInstance
(
"PKCS12"
);
KeyStore
keyStore
=
KeyStore
.
getInstance
(
"PKCS12"
);
temp1
=
asFile
(
readJarFile
(
classesPath
+
"/qg-keystore.jks"
));
keyStore
.
load
(
HttpClientConfig
.
class
.
getResourceAsStream
(
classesPath
+
"/qg-keystore.jks"
),
"password"
.
toCharArray
(
));
keyStore
.
load
(
new
FileInputStream
(
temp1
),
"password"
.
toCharArray
()
);
KeyStore
trustStore
=
KeyStore
.
getInstance
(
"JKS"
);
temp2
=
asFile
(
readJarFile
(
classesPath
+
"/qg-truststore.jks"
));
trustStore
.
load
(
HttpClientConfig
.
class
.
getResourceAsStream
(
classesPath
+
"/qg-truststore.jks"
),
"password"
.
toCharArray
(
));
SSLContext
sslcontext
=
SSLContexts
.
custom
()
SSLContext
sslcontext
=
SSLContexts
.
custom
()
.
loadTrustMaterial
(
temp2
,
"password"
.
toCharArray
()
,
new
TrustSelfSignedStrategy
())
.
loadTrustMaterial
(
trustStore
,
new
TrustSelfSignedStrategy
())
.
loadKeyMaterial
(
keyStore
,
"password"
.
toCharArray
())
.
loadKeyMaterial
(
keyStore
,
"password"
.
toCharArray
())
.
build
();
.
build
();
SSLConnectionSocketFactory
sslConnectionSocketFactory
=
new
SSLConnectionSocketFactory
(
SSLConnectionSocketFactory
sslConnectionSocketFactory
=
new
SSLConnectionSocketFactory
(
sslcontext
,
sslcontext
,
new
String
[]{
"TLSv1.2"
},
new
String
[]{
"TLSv1.2"
},
null
,
null
,
SSLConnectionSocketFactory
.
getDefaultHostnameVerifier
());
SSLConnectionSocketFactory
.
getDefaultHostnameVerifier
());
Registry
<
ConnectionSocketFactory
>
socketFactoryRegistry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
Registry
<
ConnectionSocketFactory
>
socketFactoryRegistry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
.
register
(
"http"
,
PlainConnectionSocketFactory
.
getSocketFactory
())
.
register
(
"http"
,
PlainConnectionSocketFactory
.
getSocketFactory
())
.
register
(
"https"
,
sslConnectionSocketFactory
)
.
register
(
"https"
,
sslConnectionSocketFactory
)
.
build
();
.
build
();
// connection manager
// connection manager
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
(
socketFactoryRegistry
);
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
(
socketFactoryRegistry
);
connectionManager
.
setMaxTotal
(
10000
);
connectionManager
.
setMaxTotal
(
10000
);
connectionManager
.
setDefaultMaxPerRoute
(
1000
);
connectionManager
.
setDefaultMaxPerRoute
(
1000
);
HttpRequestRetryHandler
retryHandler
=
new
HttpRequestRetryHandler
()
{
HttpRequestRetryHandler
retryHandler
=
new
HttpRequestRetryHandler
()
{
@Override
@Override
public
boolean
retryRequest
(
IOException
arg0
,
int
retryTimes
,
HttpContext
arg2
)
{
public
boolean
retryRequest
(
IOException
arg0
,
int
retryTimes
,
HttpContext
arg2
)
{
if
(
retryTimes
>=
2
)
if
(
retryTimes
>=
2
)
return
false
;
if
(
arg0
instanceof
UnknownHostException
||
arg0
instanceof
ConnectTimeoutException
||
!(
arg0
instanceof
SSLException
)
||
arg0
instanceof
SocketTimeoutException
)
return
true
;
HttpClientContext
clientContext
=
HttpClientContext
.
adapt
(
arg2
);
HttpRequest
request
=
clientContext
.
getRequest
();
if
(!(
request
instanceof
HttpEntityEnclosingRequest
))
// 如果请求被认为是幂等的,那么就重试。即重复执行不影响程序其他效果的
return
true
;
return
false
;
return
false
;
}
if
(
arg0
instanceof
UnknownHostException
||
arg0
instanceof
ConnectTimeoutException
};
||
!(
arg0
instanceof
SSLException
)
||
arg0
instanceof
SocketTimeoutException
)
// keep alive strategy
return
true
;
ConnectionKeepAliveStrategy
keepAliveStrategy
=
new
DefaultConnectionKeepAliveStrategy
();
HttpClientContext
clientContext
=
HttpClientContext
.
adapt
(
arg2
);
return
HttpClients
.
custom
()
HttpRequest
request
=
clientContext
.
getRequest
();
.
setConnectionManager
(
connectionManager
)
if
(!(
request
instanceof
HttpEntityEnclosingRequest
))
// 如果请求被认为是幂等的,那么就重试。即重复执行不影响程序其他效果的
.
setRetryHandler
(
retryHandler
)
return
true
;
.
setKeepAliveStrategy
(
keepAliveStrategy
)
return
false
;
.
build
();
}
finally
{
if
(
temp1
!=
null
){
temp1
.
delete
();
}
if
(
temp2
!=
null
){
temp2
.
delete
();
}
}
}
};
// keep alive strategy
}
ConnectionKeepAliveStrategy
keepAliveStrategy
=
new
DefaultConnectionKeepAliveStrategy
();
return
HttpClients
.
custom
()
.
setConnectionManager
(
connectionManager
)
.
setRetryHandler
(
retryHandler
)
.
setKeepAliveStrategy
(
keepAliveStrategy
)
.
build
();
private
static
InputStream
readJarFile
(
String
fileName
)
throws
IOException
{
return
HttpClientConfig
.
class
.
getResourceAsStream
(
fileName
);
}
}
public
static
File
asFile
(
InputStream
inputStream
)
throws
IOException
{
File
tmp
=
File
.
createTempFile
(
"trustKey"
,
".jks"
,
new
File
(
""
));
OutputStream
os
=
new
FileOutputStream
(
tmp
);
int
bytesRead
=
0
;
byte
[]
buffer
=
new
byte
[
8192
];
while
((
bytesRead
=
inputStream
.
read
(
buffer
,
0
,
8192
))
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
bytesRead
);
}
inputStream
.
close
();
return
tmp
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment