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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
32 deletions
+19
-32
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
+18
-31
No files found.
sobot_network/sobot-network-publish-mavencentral.gradle
View file @
644494f8
...
@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
...
@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext
{
ext
{
PUBLISH_GROUP_ID
=
"com.sobot.library"
//项目包名
PUBLISH_GROUP_ID
=
"com.sobot.library"
//项目包名
PUBLISH_ARTIFACT_ID
=
'net'
//项目名
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
;
package
com
.
sobot
.
network
.
http
;
import
static
okhttp3
.
internal
.
Util
.
assertionError
;
import
android.annotation.SuppressLint
;
import
android.annotation.SuppressLint
;
import
android.os.Handler
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.os.Looper
;
...
@@ -14,13 +16,12 @@ import com.sobot.network.http.builder.PostStringBuilder;
...
@@ -14,13 +16,12 @@ import com.sobot.network.http.builder.PostStringBuilder;
import
com.sobot.network.http.callback.Callback
;
import
com.sobot.network.http.callback.Callback
;
import
com.sobot.network.http.callback.FileCallBack
;
import
com.sobot.network.http.callback.FileCallBack
;
import
com.sobot.network.http.log.LoggerInterceptor
;
import
com.sobot.network.http.log.LoggerInterceptor
;
import
com.sobot.network.http.log.SobotNetLogUtils
;
import
com.sobot.network.http.request.RequestCall
;
import
com.sobot.network.http.request.RequestCall
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.security.GeneralSecurityException
;
import
java.security.KeyStore
;
import
java.security.KeyStore
;
import
java.security.SecureRandom
;
import
java.security.SecureRandom
;
import
java.security.cert.X509Certificate
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -49,15 +50,7 @@ public class SobotOkHttpUtils {
...
@@ -49,15 +50,7 @@ public class SobotOkHttpUtils {
okHttpClientBuilder
.
addInterceptor
(
new
SobotInternetPermissionExceptionInterceptor
());
okHttpClientBuilder
.
addInterceptor
(
new
SobotInternetPermissionExceptionInterceptor
());
mDelivery
=
new
Handler
(
Looper
.
getMainLooper
());
mDelivery
=
new
Handler
(
Looper
.
getMainLooper
());
try
{
try
{
//优化X509TrustManager安全警告问题
X509TrustManager
manager
=
platformTrustManager
();
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
];
okHttpClientBuilder
.
sslSocketFactory
(
createSSLSocketFactory
(
manager
),
manager
);
okHttpClientBuilder
.
sslSocketFactory
(
createSSLSocketFactory
(
manager
),
manager
);
mOkHttpClient
=
okHttpClientBuilder
.
build
();
mOkHttpClient
=
okHttpClientBuilder
.
build
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -291,13 +284,13 @@ public class SobotOkHttpUtils {
...
@@ -291,13 +284,13 @@ public class SobotOkHttpUtils {
* @return
* @return
*/
*/
@SuppressLint
(
"TrulyRandom"
)
@SuppressLint
(
"TrulyRandom"
)
private
static
SSLSocketFactory
createSSLSocketFactory
(
X509TrustManager
m
anager
)
{
private
static
SSLSocketFactory
createSSLSocketFactory
(
X509TrustManager
trustM
anager
)
{
SSLSocketFactory
sSLSocketFactory
=
null
;
SSLSocketFactory
sSLSocketFactory
=
null
;
try
{
try
{
SSLContext
sc
=
SSLContext
.
getInstance
(
"TLS"
);
SSLContext
sc
=
SSLContext
.
getInstance
(
"TLS"
);
sc
.
init
(
null
,
getTrustManager
()
,
sc
.
init
(
null
,
new
TrustManager
[]{
trustManager
}
,
new
SecureRandom
());
new
SecureRandom
());
sSLSocketFactory
=
sc
.
getSocketFactory
();
sSLSocketFactory
=
sc
.
getSocketFactory
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -307,25 +300,20 @@ public class SobotOkHttpUtils {
...
@@ -307,25 +300,20 @@ public class SobotOkHttpUtils {
return
sSLSocketFactory
;
return
sSLSocketFactory
;
}
}
//获取TrustManager
public
static
X509TrustManager
platformTrustManager
()
{
private
static
TrustManager
[]
getTrustManager
()
{
try
{
TrustManager
[]
trustAllCerts
=
new
TrustManager
[]{
TrustManagerFactory
trustManagerFactory
=
TrustManagerFactory
.
getInstance
(
new
X509TrustManager
()
{
TrustManagerFactory
.
getDefaultAlgorithm
());
@Override
trustManagerFactory
.
init
((
KeyStore
)
null
);
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
TrustManager
[]
trustManagers
=
trustManagerFactory
.
getTrustManagers
();
}
if
(
trustManagers
.
length
!=
1
||
!(
trustManagers
[
0
]
instanceof
X509TrustManager
))
{
throw
new
IllegalStateException
(
"Unexpected default trust managers:"
@Override
+
Arrays
.
toString
(
trustManagers
));
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
}
@Override
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
new
X509Certificate
[]{};
}
}
return
(
X509TrustManager
)
trustManagers
[
0
];
}
catch
(
GeneralSecurityException
e
)
{
throw
assertionError
(
"No System TLS"
,
e
);
// The system has no TLS. Just give up.
}
}
};
return
trustAllCerts
;
}
}
}
}
\ 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