Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds-android
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
云深项目组
beyond-clouds-android
Commits
fd9ae6fb
Commit
fd9ae6fb
authored
Mar 17, 2020
by
段启岩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加请求拦截器
parent
fb6a1389
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
2 deletions
+103
-2
app/src/main/java/cn/yunliyunwai/beyondclouds/data/TokenInterceptor.java
+36
-0
app/src/main/java/cn/yunliyunwai/beyondclouds/di/module/AppModule.java
+6
-2
app/src/main/java/cn/yunliyunwai/beyondclouds/viewmodel/AuthenticationViewModel.java
+61
-0
No files found.
app/src/main/java/cn/yunliyunwai/beyondclouds/data/TokenInterceptor.java
0 → 100644
View file @
fd9ae6fb
package
cn
.
yunliyunwai
.
beyondclouds
.
data
;
import
android.util.Log
;
import
java.io.IOException
;
import
javax.inject.Inject
;
import
javax.inject.Singleton
;
import
cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel
;
import
okhttp3.Interceptor
;
import
okhttp3.Request
;
import
okhttp3.Response
;
@Singleton
public
class
TokenInterceptor
implements
Interceptor
{
private
AuthenticationViewModel
authenticationViewModel
;
@Inject
public
TokenInterceptor
(
AuthenticationViewModel
authenticationViewModel
)
{
this
.
authenticationViewModel
=
authenticationViewModel
;
}
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Request
.
Builder
requestBuilder
=
chain
.
request
().
newBuilder
();
if
(
authenticationViewModel
.
getAuthenticationState
().
getValue
()
==
AuthenticationViewModel
.
AuthenticationState
.
AUTHENTICATED
)
{
requestBuilder
.
addHeader
(
"Authorization"
,
"Bearer "
+
authenticationViewModel
.
getToken
());
Log
.
d
(
getClass
().
getName
(),
"检测到用户已认证,本次请求将携带token:"
+
authenticationViewModel
.
getToken
());
}
else
{
Log
.
d
(
getClass
().
getName
(),
"检测到用未认证,本次请求不携带token!"
);
}
return
chain
.
proceed
(
requestBuilder
.
build
());
}
}
app/src/main/java/cn/yunliyunwai/beyondclouds/di/module/AppModule.java
View file @
fd9ae6fb
...
...
@@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit;
import
javax.inject.Singleton
;
import
cn.yunliyunwai.beyondclouds.adapter.common.LiveDataCallAdapterFactory
;
import
cn.yunliyunwai.beyondclouds.data.TokenInterceptor
;
import
cn.yunliyunwai.beyondclouds.data.source.local.database.BlogDatabase
;
import
cn.yunliyunwai.beyondclouds.data.source.local.database.ProjectDatabase
;
import
cn.yunliyunwai.beyondclouds.data.source.local.database.QuestionDatabase
;
...
...
@@ -89,9 +90,12 @@ public class AppModule {
@Singleton
@Provides
public
OkHttpClient
provideOkHttpClient
()
{
public
OkHttpClient
provideOkHttpClient
(
TokenInterceptor
tokenInterceptor
)
{
OkHttpClient
.
Builder
clientBuilder
=
new
OkHttpClient
.
Builder
()
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
);
.
connectTimeout
(
10
,
TimeUnit
.
SECONDS
)
.
readTimeout
(
20
,
TimeUnit
.
SECONDS
)
.
writeTimeout
(
20
,
TimeUnit
.
SECONDS
)
.
addInterceptor
(
tokenInterceptor
);
return
clientBuilder
.
build
();
}
...
...
app/src/main/java/cn/yunliyunwai/beyondclouds/viewmodel/AuthenticationViewModel.java
0 → 100644
View file @
fd9ae6fb
package
cn
.
yunliyunwai
.
beyondclouds
.
viewmodel
;
import
android.content.Context
;
import
androidx.lifecycle.LiveData
;
import
androidx.lifecycle.MutableLiveData
;
import
javax.inject.Inject
;
import
javax.inject.Singleton
;
@Singleton
public
class
AuthenticationViewModel
{
private
static
final
String
SHARED_PREFERENCES_NAME_AUTHENTICATION
=
"SHARED_PREFERENCES_AUTHENTICATION"
;
private
static
final
String
SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN
=
"SHARED_PREFERENCES_AUTHENTICATION_TOKEN"
;
private
String
token
;
private
MutableLiveData
<
AuthenticationState
>
authenticationState
=
new
MutableLiveData
<>();
public
static
enum
AuthenticationState
{
/**
* 未认证
*/
UN_AUTHENTICATION
,
/**
* 已认证
*/
AUTHENTICATED
}
@Inject
public
AuthenticationViewModel
(
Context
context
)
{
String
token
=
context
.
getSharedPreferences
(
SHARED_PREFERENCES_NAME_AUTHENTICATION
,
Context
.
MODE_PRIVATE
).
getString
(
SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN
,
null
);
if
(
null
!=
token
)
{
this
.
token
=
token
;
this
.
authenticationState
.
setValue
(
AuthenticationState
.
AUTHENTICATED
);
}
else
{
this
.
authenticationState
.
setValue
(
AuthenticationState
.
UN_AUTHENTICATION
);
}
}
public
LiveData
<
AuthenticationState
>
getAuthenticationState
()
{
return
authenticationState
;
}
public
String
getToken
()
{
return
token
;
}
/**
* 用户登录
* @param account
* @param password
*/
public
void
login
(
String
account
,
String
password
)
{
}
}
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