Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
MiYaFastCashier
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangjiantao
MiYaFastCashier
Commits
0e191dd7
Commit
0e191dd7
authored
May 19, 2022
by
赵鹏翔
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加okhttp证书校验,上传日志时禁止写入
parent
c1d3e9a0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
208 additions
and
18 deletions
+208
-18
BaseApplication.kt
app/src/main/java/com/miya/fastcashier/BaseApplication.kt
+30
-16
ApiConfig.kt
app/src/main/java/com/miya/fastcashier/net/ApiConfig.kt
+1
-1
ApiRequest.kt
app/src/main/java/com/miya/fastcashier/net/ApiRequest.kt
+12
-1
SSLCertificatesInit.java
...in/java/com/miya/fastcashier/net/SSLCertificatesInit.java
+165
-0
No files found.
app/src/main/java/com/miya/fastcashier/BaseApplication.kt
View file @
0e191dd7
...
...
@@ -116,46 +116,60 @@ class BaseApplication : MultiDexApplication() {
override
fun
onActivityCreated
(
activity
:
Activity
,
savedInstanceState
:
Bundle
?)
{
DensityUtils
.
setDensity
(
activity
,
this
@BaseApplication
)
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityCreated"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityCreated() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityCreated() called with: activity = [$activity]\n"
)
}
}
override
fun
onActivityStarted
(
activity
:
Activity
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityStarted"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityStarted() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityStarted() called with: activity = [$activity]\n"
)
}
}
override
fun
onActivityResumed
(
activity
:
Activity
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityResumed"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityResumed() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityResumed() called with: activity = [$activity]\n"
)
}
}
override
fun
onActivityPaused
(
activity
:
Activity
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityPaused"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityPaused() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityPaused() called with: activity = [$activity]\n"
)
}
}
override
fun
onActivityStopped
(
activity
:
Activity
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityStopped"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityStopped() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityStopped() called with: activity = [$activity]\n"
)
}
}
override
fun
onActivitySaveInstanceState
(
activity
:
Activity
,
outState
:
Bundle
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivitySaveInstanceState"
)
val
memoryStatus
=
"\ttotalMemory:${Runtime.getRuntime().totalMemory()}\tfreeMemory:${Runtime.getRuntime().freeMemory()}"
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivitySaveInstanceState() called with:"
+
" activity = [$activity]\nmemoryStatus: $memoryStatus"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
val
memoryStatus
=
"\ttotalMemory:${Runtime.getRuntime().totalMemory()}\tfreeMemory:${Runtime.getRuntime().freeMemory()}"
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivitySaveInstanceState() called with:"
+
" activity = [$activity]\nmemoryStatus: $memoryStatus"
)
}
}
override
fun
onActivityDestroyed
(
activity
:
Activity
)
{
XLog
.
d
(
DateUtils
.
format18
(
Date
())
+
" onActivityDestroyed"
)
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityDestroyed() called with: activity = [$activity]\n"
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
ContextUtils
.
getContext
(),
"\n${DateUtils.format18(Date())}########onActivityDestroyed() called with: activity = [$activity]\n"
)
}
}
})
}
...
...
app/src/main/java/com/miya/fastcashier/net/ApiConfig.kt
View file @
0e191dd7
...
...
@@ -6,7 +6,7 @@ import com.miya.fastcashier.beans.SelfCashierTerminalConfig
object
ApiConfig
{
private
const
val
BASE_URL
=
"https://hhms.miyapay.com/"
private
const
val
BASE_URL_4_TEST
=
""
private
const
val
BASE_URL_4_TEST
=
"
https://hhmspre.miyapay.com/
"
@JvmStatic
val
baseUrl
:
String
...
...
app/src/main/java/com/miya/fastcashier/net/ApiRequest.kt
View file @
0e191dd7
...
...
@@ -9,6 +9,9 @@ import okhttp3.MultipartBody
import
okhttp3.OkHttpClient
import
retrofit2.Retrofit
import
retrofit2.converter.gson.GsonConverterFactory
import
java.util.concurrent.TimeUnit
import
javax.net.ssl.SSLSocketFactory
import
javax.net.ssl.X509TrustManager
class
ApiRequest
private
constructor
()
{
...
...
@@ -31,12 +34,20 @@ class ApiRequest private constructor() {
val
httpLoggingInterceptor
=
MiyaHttpLoggingInterceptor
{
Log
.
e
(
"####"
,
it
)
LogFileUtils
.
writeLog
(
BaseApplication
.
getApplication
(),
it
)
if
(!
LogFileUtils
.
isProhibitWrite
)
{
LogFileUtils
.
writeLog
(
BaseApplication
.
getApplication
(),
it
)
}
}.
apply
{
level
=
MiyaHttpLoggingInterceptor
.
Level
.
BODY
}
val
socketFactory
=
arrayOfNulls
<
SSLSocketFactory
>(
1
)
val
trustManager
=
arrayOfNulls
<
X509TrustManager
>(
1
)
SSLCertificatesInit
.
init
(
socketFactory
,
trustManager
)
val
client
=
OkHttpClient
.
Builder
()
.
addInterceptor
(
httpLoggingInterceptor
)
.
addInterceptor
(
RequestSignInterceptor
())
.
sslSocketFactory
(
socketFactory
[
0
]
!!
,
trustManager
[
0
]
!!
)
.
build
()
val
retrofit
=
Retrofit
.
Builder
()
...
...
app/src/main/java/com/miya/fastcashier/net/SSLCertificatesInit.java
0 → 100644
View file @
0e191dd7
package
com
.
miya
.
fastcashier
.
net
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.security.KeyManagementException
;
import
java.security.KeyStore
;
import
java.security.KeyStoreException
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.SecureRandom
;
import
java.security.UnrecoverableKeyException
;
import
java.security.cert.CertificateException
;
import
java.security.cert.CertificateFactory
;
import
java.security.cert.X509Certificate
;
import
javax.net.ssl.KeyManager
;
import
javax.net.ssl.KeyManagerFactory
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.SSLSocketFactory
;
import
javax.net.ssl.TrustManager
;
import
javax.net.ssl.TrustManagerFactory
;
import
javax.net.ssl.X509TrustManager
;
public
class
SSLCertificatesInit
{
public
static
final
String
TLS
=
"TLS"
;
public
static
final
String
SSL
=
"SSL"
;
public
static
final
String
SSLV2
=
"SSLv2"
;
private
static
final
String
SERVER_CERTIFICATE_TYPE
=
"X.509"
;
private
static
final
String
CLIENT_CERTIFICATE_TYPE
=
"BKS"
;
/**
* 信任所有站点
*
* @param socketFactory 输出参数,在外部创建1个SSLSocketFactory数组
* @param trustManager 输出参数,在外部创建1个X509TrustManager数组
*/
public
static
void
init
(
SSLSocketFactory
[]
socketFactory
,
X509TrustManager
[]
trustManager
)
{
try
{
SSLContext
sslContext
=
SSLContext
.
getInstance
(
TLS
);
trustManager
[
0
]
=
new
X509TrustManager
()
{
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
new
X509Certificate
[]{};
}
public
void
checkClientTrusted
(
X509Certificate
[]
certs
,
String
authType
)
{
}
public
void
checkServerTrusted
(
X509Certificate
[]
certs
,
String
authType
)
{
}
};
sslContext
.
init
(
null
,
new
TrustManager
[]{
trustManager
[
0
]},
new
SecureRandom
());
socketFactory
[
0
]
=
sslContext
.
getSocketFactory
();
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
}
/**
* 单向验证
*
* @param socketFactory 输出参数,在外部创建1个SSLSocketFactory数组
* @param trustManager 输出参数,在外部创建1个X509TrustManager数组
* @param in 服务器cer证书文件的InputStream对象
*/
public
static
void
init
(
SSLSocketFactory
[]
socketFactory
,
X509TrustManager
[]
trustManager
,
InputStream
in
)
{
try
{
SSLContext
sslContext
=
SSLContext
.
getInstance
(
TLS
);
TrustManager
[]
tmArr
=
createTrustManagers
(
genServerKeyStore
(
in
));
sslContext
.
init
(
null
,
tmArr
,
new
SecureRandom
());
trustManager
[
0
]
=
getX509TrustManager
(
tmArr
);
socketFactory
[
0
]
=
sslContext
.
getSocketFactory
();
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyStoreException
e
)
{
e
.
printStackTrace
();
}
catch
(
CertificateException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
in
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
private
static
TrustManager
[]
createTrustManagers
(
KeyStore
ks
)
throws
KeyStoreException
,
NoSuchAlgorithmException
{
TrustManagerFactory
tmf
=
TrustManagerFactory
.
getInstance
(
TrustManagerFactory
.
getDefaultAlgorithm
());
tmf
.
init
(
ks
);
return
tmf
.
getTrustManagers
();
}
private
static
KeyStore
genServerKeyStore
(
InputStream
in
)
throws
KeyStoreException
,
CertificateException
,
NoSuchAlgorithmException
,
IOException
{
KeyStore
keyStore
=
KeyStore
.
getInstance
(
KeyStore
.
getDefaultType
());
keyStore
.
load
(
null
);
CertificateFactory
cf
=
CertificateFactory
.
getInstance
(
SERVER_CERTIFICATE_TYPE
);
keyStore
.
setCertificateEntry
(
SERVER_CERTIFICATE_TYPE
,
cf
.
generateCertificate
(
in
));
return
keyStore
;
}
/**
* 双向验证
*
* @param socketFactory 输出参数,在外部创建1个SSLSocketFactory数组
* @param trustManager 输出参数,在外部创建1个X509TrustManager数组
* @param serverIn 服务器cer证书文件的InputStream对象
* @param clientIn 客户端bks证书的InputStream对象
* @param clientPwd 客户端bks证书的密码
*/
public
static
void
getSocketFactory
(
SSLSocketFactory
[]
socketFactory
,
X509TrustManager
[]
trustManager
,
InputStream
serverIn
,
InputStream
clientIn
,
String
clientPwd
)
{
try
{
SSLContext
sslContext
=
SSLContext
.
getInstance
(
TLS
);
TrustManager
[]
tmArr
=
createTrustManagers
(
genServerKeyStore
(
serverIn
));
sslContext
.
init
(
createKeyManagers
(
genClientKeyStore
(
clientIn
,
clientPwd
),
clientPwd
),
tmArr
,
new
SecureRandom
());
trustManager
[
0
]
=
getX509TrustManager
(
tmArr
);
socketFactory
[
0
]
=
sslContext
.
getSocketFactory
();
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyStoreException
e
)
{
e
.
printStackTrace
();
}
catch
(
CertificateException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
UnrecoverableKeyException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
serverIn
.
close
();
clientIn
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
private
static
KeyManager
[]
createKeyManagers
(
KeyStore
ks
,
String
pwd
)
throws
KeyStoreException
,
NoSuchAlgorithmException
,
UnrecoverableKeyException
{
KeyManagerFactory
kmf
=
KeyManagerFactory
.
getInstance
(
KeyManagerFactory
.
getDefaultAlgorithm
());
kmf
.
init
(
ks
,
pwd
.
toCharArray
());
return
kmf
.
getKeyManagers
();
}
private
static
KeyStore
genClientKeyStore
(
InputStream
in
,
String
pwd
)
throws
KeyStoreException
,
CertificateException
,
NoSuchAlgorithmException
,
IOException
{
KeyStore
ks
=
KeyStore
.
getInstance
(
CLIENT_CERTIFICATE_TYPE
);
ks
.
load
(
in
,
pwd
.
toCharArray
());
return
ks
;
}
private
static
X509TrustManager
getX509TrustManager
(
TrustManager
[]
tmArr
)
{
for
(
TrustManager
tm
:
tmArr
)
{
if
(
tm
instanceof
X509TrustManager
)
{
return
(
X509TrustManager
)
tm
;
}
}
return
null
;
}
}
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