Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Sobot_module_Dev
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sobot_android
Sobot_module_Dev
Commits
644494f8
Commit
644494f8
authored
Nov 29, 2023
by
zhengnw@sobot.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net 1.2.1 优化checkServerTrusted 漏洞问题,(参考
https://blog.csdn.net/kyzycyey/article/details/130065853)
parent
e67e7fac
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
33 deletions
+22
-33
sobot-network-publish-mavencentral.gradle
sobot_network/sobot-network-publish-mavencentral.gradle
+1
-1
SobotOkHttpUtils.java
...rc/main/java/com/sobot/network/http/SobotOkHttpUtils.java
+21
-32
No files found.
sobot_network/sobot-network-publish-mavencentral.gradle
View file @
644494f8
...
...
@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext
{
PUBLISH_GROUP_ID
=
"com.sobot.library"
//项目包名
PUBLISH_ARTIFACT_ID
=
'net'
//项目名
PUBLISH_VERSION
=
'1.2.
0
'
//版本号
PUBLISH_VERSION
=
'1.2.
1
'
//版本号
}
...
...
sobot_network/src/main/java/com/sobot/network/http/SobotOkHttpUtils.java
View file @
644494f8
package
com
.
sobot
.
network
.
http
;
import
static
okhttp3
.
internal
.
Util
.
assertionError
;
import
android.annotation.SuppressLint
;
import
android.os.Handler
;
import
android.os.Looper
;
...
...
@@ -14,13 +16,12 @@ import com.sobot.network.http.builder.PostStringBuilder;
import
com.sobot.network.http.callback.Callback
;
import
com.sobot.network.http.callback.FileCallBack
;
import
com.sobot.network.http.log.LoggerInterceptor
;
import
com.sobot.network.http.log.SobotNetLogUtils
;
import
com.sobot.network.http.request.RequestCall
;
import
java.io.IOException
;
import
java.security.GeneralSecurityException
;
import
java.security.KeyStore
;
import
java.security.SecureRandom
;
import
java.security.cert.X509Certificate
;
import
java.util.Arrays
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -49,15 +50,7 @@ public class SobotOkHttpUtils {
okHttpClientBuilder
.
addInterceptor
(
new
SobotInternetPermissionExceptionInterceptor
());
mDelivery
=
new
Handler
(
Looper
.
getMainLooper
());
try
{
//优化X509TrustManager安全警告问题
TrustManagerFactory
trustManagerFactory
=
TrustManagerFactory
.
getInstance
(
TrustManagerFactory
.
getDefaultAlgorithm
());
trustManagerFactory
.
init
((
KeyStore
)
null
);
TrustManager
[]
trustManagers
=
trustManagerFactory
.
getTrustManagers
();
if
(
trustManagers
.
length
!=
1
||
!(
trustManagers
[
0
]
instanceof
X509TrustManager
))
{
SobotNetLogUtils
.
e
(
"Unexpected default trust managers:"
+
Arrays
.
toString
(
trustManagers
));
}
X509TrustManager
manager
=
(
X509TrustManager
)
trustManagers
[
0
];
X509TrustManager
manager
=
platformTrustManager
();
okHttpClientBuilder
.
sslSocketFactory
(
createSSLSocketFactory
(
manager
),
manager
);
mOkHttpClient
=
okHttpClientBuilder
.
build
();
}
catch
(
Exception
e
)
{
...
...
@@ -291,13 +284,13 @@ public class SobotOkHttpUtils {
* @return
*/
@SuppressLint
(
"TrulyRandom"
)
private
static
SSLSocketFactory
createSSLSocketFactory
(
X509TrustManager
m
anager
)
{
private
static
SSLSocketFactory
createSSLSocketFactory
(
X509TrustManager
trustM
anager
)
{
SSLSocketFactory
sSLSocketFactory
=
null
;
try
{
SSLContext
sc
=
SSLContext
.
getInstance
(
"TLS"
);
sc
.
init
(
null
,
getTrustManager
()
,
sc
.
init
(
null
,
new
TrustManager
[]{
trustManager
}
,
new
SecureRandom
());
sSLSocketFactory
=
sc
.
getSocketFactory
();
}
catch
(
Exception
e
)
{
...
...
@@ -307,25 +300,20 @@ public class SobotOkHttpUtils {
return
sSLSocketFactory
;
}
//获取TrustManager
private
static
TrustManager
[]
getTrustManager
()
{
TrustManager
[]
trustAllCerts
=
new
TrustManager
[]{
new
X509TrustManager
()
{
@Override
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
}
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
}
@Override
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
new
X509Certificate
[]{};
}
}
};
return
trustAllCerts
;
public
static
X509TrustManager
platformTrustManager
()
{
try
{
TrustManagerFactory
trustManagerFactory
=
TrustManagerFactory
.
getInstance
(
TrustManagerFactory
.
getDefaultAlgorithm
());
trustManagerFactory
.
init
((
KeyStore
)
null
);
TrustManager
[]
trustManagers
=
trustManagerFactory
.
getTrustManagers
();
if
(
trustManagers
.
length
!=
1
||
!(
trustManagers
[
0
]
instanceof
X509TrustManager
))
{
throw
new
IllegalStateException
(
"Unexpected default trust managers:"
+
Arrays
.
toString
(
trustManagers
));
}
return
(
X509TrustManager
)
trustManagers
[
0
];
}
catch
(
GeneralSecurityException
e
)
{
throw
assertionError
(
"No System TLS"
,
e
);
// The system has no TLS. Just give up.
}
}
}
\ No newline at end of file
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