Browse Source

sample: 新增刷新token拦截器

drake 2 years ago
parent
commit
017816acad

+ 33 - 4
docs/interceptor.md

@@ -1,9 +1,13 @@
-拦截器(Interceptor)一般用于修改请求的参数或者进行请求转发/重试. Net就是使用的OkHttp的拦截器. 所以支持市面上的所有OkHttp拦截器组件库
+Net总共支持两种拦截器
+
+1. Interceptor, 支持市面上的所有OkHttp拦截器组件库, 可以修改任何请求响应信息, 可以转发请求
+2. RequestInterceptor, 更简单易用的轻量拦截器, 一般用于添加全局请求头/参数, 无法转发请求
 <br>
 <br>
 
 
-> 你的业务可能需要请求参数加密或者响应信息需要解密. 请尽可能不要封装Post/Get等请求动作(这是不明智的做法) <br>
-  自定义拦截器和转换器可以应对任何项目需求. 同时更符合项目设计
+> 你的业务可能需要加密解密. 请不要封装Post/Get等请求动作(这是不明智的做法) <br>
+  建议自定义拦截器和转换器可以应对任何项目需求, 同时更符合架构设计
 
 
+添加拦截器
 
 
 ```kotlin
 ```kotlin
 class App : Application() {
 class App : Application() {
@@ -11,12 +15,37 @@ class App : Application() {
         super.onCreate()
         super.onCreate()
 
 
         NetConfig.initialize("https://github.com/liangjingkanji/Net/", this) {
         NetConfig.initialize("https://github.com/liangjingkanji/Net/", this) {
-            addInterceptor { chain -> chain.proceed(chain.request()) }
+            addInterceptor(RefreshTokenInterceptor())
+        }
+    }
+}
+```
+
+以下为简单演示客户端自动刷新token拦截器
+
+```kotlin
+/**
+ * 演示如何自动刷新token令牌
+ */
+class RefreshTokenInterceptor : Interceptor {
+    override fun intercept(chain: Interceptor.Chain): Response {
+        val request = chain.request()
+        val response = chain.proceed(request) // 如果token失效
+
+        return synchronized(RefreshTokenInterceptor::class.java) {
+            if (response.code == 401 && UserConfig.isLogin && !request.url.pathSegments.contains("token")) {
+                val json = Net.get("token").execute<String>() // 同步刷新token
+                UserConfig.token = JSONObject(json).optString("token")
+                chain.proceed(request)
+            } else {
+                response
+            }
         }
         }
     }
     }
 }
 }
 ```
 ```
 
 
+
 在拦截器中可以使用以下函数复制请求/响应体
 在拦截器中可以使用以下函数复制请求/响应体
 
 
 | 函数 | 描述 |
 | 函数 | 描述 |

+ 29 - 0
sample/src/main/java/com/drake/net/sample/constants/UserConfig.kt

@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2018 Drake, https://github.com/liangjingkanji
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.drake.net.sample.constants
+
+
+/**
+ * 建议使用 https://github.com/liangjingkanji/Serialize 为字段提供持久化存储
+ */
+object UserConfig {
+
+    var token = "6cad0ff06f5a214b9cfdf2a4a7c432339"
+
+    var isLogin = true
+}

+ 2 - 1
sample/src/main/java/com/drake/net/sample/interfaces/MyRequestInterceptor.kt

@@ -2,6 +2,7 @@ package com.drake.net.sample.interfaces
 
 
 import com.drake.net.interceptor.RequestInterceptor
 import com.drake.net.interceptor.RequestInterceptor
 import com.drake.net.request.BaseRequest
 import com.drake.net.request.BaseRequest
+import com.drake.net.sample.constants.UserConfig
 
 
 
 
 /** 请求拦截器, 一般用于添加全局参数 */
 /** 请求拦截器, 一般用于添加全局参数 */
@@ -13,6 +14,6 @@ class MyRequestInterceptor : RequestInterceptor {
         if (request.headers()["client"] == null) {
         if (request.headers()["client"] == null) {
             request.addHeader("client", "Android")
             request.addHeader("client", "Android")
         }
         }
-        request.setHeader("token", "6cad0ff06f5a214b9cfdf2a4a7c43339")
+        request.setHeader("token", UserConfig.token)
     }
     }
 }
 }

+ 45 - 0
sample/src/main/java/com/drake/net/sample/interfaces/RefreshTokenInterceptor.kt

@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2018 Drake, https://github.com/liangjingkanji
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.drake.net.sample.interfaces
+
+import com.drake.net.Net
+import com.drake.net.sample.constants.UserConfig
+import okhttp3.Interceptor
+import okhttp3.Response
+import org.json.JSONObject
+
+
+/**
+ * 演示如何自动刷新token令牌
+ */
+class RefreshTokenInterceptor : Interceptor {
+    override fun intercept(chain: Interceptor.Chain): Response {
+        val request = chain.request()
+        val response = chain.proceed(request) // 如果token失效
+
+        return synchronized(RefreshTokenInterceptor::class.java) {
+            if (response.code == 401 && UserConfig.isLogin && !request.url.pathSegments.contains("token")) {
+                val json = Net.get("token").execute<String>() // 同步刷新token
+                UserConfig.token = JSONObject(json).optString("token")
+                chain.proceed(request)
+            } else {
+                response
+            }
+        }
+    }
+}