-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
487 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 0 additions & 131 deletions
131
SmartToken/src/main/java/wang/unclecat/smarttoken/AbsAccessTokenInterceptor.java
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
SmartToken/src/main/java/wang/unclecat/smarttoken/ActiveSmartToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package wang.unclecat.smarttoken; | ||
|
||
import android.util.Log; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* 拦截器实现access token自动获取(主动判断是否过期) | ||
* | ||
* @author: 喵叔catuncle | ||
* @date: 2020/12/16 17:32 | ||
*/ | ||
public abstract class ActiveSmartToken extends SmartToken implements Interceptor { | ||
|
||
public ActiveSmartToken(@Type int tokenType) { | ||
super(tokenType); | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
request.url().toString(); | ||
Log.d("SmartToken", "url(处理前) = " + request.url()); | ||
Log.d("SmartToken", "headers(处理前) = " + request.headers()); | ||
|
||
String localToken = getAccessTokenFromLocal(); | ||
String token = isOutOfDate(localToken) ? getAccessTokenFromServer() : localToken; | ||
Request updatedRequest = updateToken(request, token); | ||
Log.d("SmartToken", "url(处理后) = " + updatedRequest.url()); | ||
Log.d("SmartToken", "headers(处理后) = " + updatedRequest.headers()); | ||
|
||
return chain.proceed(updatedRequest); | ||
} | ||
|
||
/** | ||
* 判断access token是否已过期 | ||
* | ||
* @param localToken 本地的token(可能为空) | ||
* @return true:过期, false:未过期 | ||
*/ | ||
protected abstract boolean isOutOfDate(String localToken); | ||
|
||
/** | ||
* 从本地同步获取access token | ||
*/ | ||
protected abstract String getAccessTokenFromLocal() throws IOException; | ||
} |
67 changes: 67 additions & 0 deletions
67
SmartToken/src/main/java/wang/unclecat/smarttoken/PassiveSmartToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package wang.unclecat.smarttoken; | ||
|
||
import android.util.Log; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okio.Buffer; | ||
import okio.BufferedSource; | ||
|
||
/** | ||
* 拦截器实现access token自动获取(通过判断结果错误码,被动获取) | ||
* | ||
* @author: 喵叔catuncle | ||
* @date: 2020/12/11 10:12 | ||
*/ | ||
public abstract class PassiveSmartToken extends SmartToken implements Interceptor { | ||
|
||
public PassiveSmartToken(@Type int tokenType) { | ||
super(tokenType); | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Response originResponse = chain.proceed(request); | ||
if (checkTokenError(originResponse)) { | ||
//同获取AccessToken | ||
String token = getAccessTokenFromServer(); | ||
|
||
//更新AccessToken | ||
Request updatedRequest = updateToken(request, token); | ||
|
||
return chain.proceed(updatedRequest); | ||
} else { | ||
return originResponse; | ||
} | ||
} | ||
|
||
private boolean checkTokenError(Response response) { | ||
ResponseBody responseBody = response.body(); | ||
BufferedSource source = responseBody.source(); | ||
|
||
try { | ||
source.request(Long.MAX_VALUE); // Buffer the entire body. | ||
Buffer buffer = source.buffer().clone(); | ||
String s = buffer.readUtf8(); | ||
return checkTokenError(s); | ||
} catch (Exception e) { | ||
Log.e("SmartToken", e.getMessage(), e); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 解析http resp body,判断是否是“access token无效”错误 | ||
* | ||
* @param respBody http 返回实体字符串 | ||
* @return true: 是, false: 否 | ||
*/ | ||
protected abstract boolean checkTokenError(String respBody); | ||
|
||
} |
Oops, something went wrong.