Skip to content

Commit

Permalink
1.降低OauthClientInterceptor中的headers优先级,不再强行替换所有已存在的请求头。
Browse files Browse the repository at this point in the history
2.把accessToken从OauthProvider的headers中拆分。
  • Loading branch information
Tears丶残阳 committed May 16, 2023
1 parent 88c5f9d commit 9dfd652
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ class OAuthClientInterceptor(private val provider: OAuthProvider) : Interceptor
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
val headers = request.headers
request = request
.newBuilder()
.removeHeader(AUTHORIZATION)
.addHeader(AUTHORIZATION, "Bearer ${provider.accessToken}")
.apply {
for ((name, value) in provider.headers) {
if (!headers.names().contains(name)) {
addHeader(name, value)
}
}
val requestBuilder = request.newBuilder()
// 添加授权
if (!provider.accessToken.isNullOrEmpty()) {
requestBuilder.removeHeader(AUTHORIZATION)
requestBuilder.addHeader(AUTHORIZATION, "Bearer ${provider.accessToken}")
}
// 添加额外的请求头,不会覆盖已存在的
val headersNames = headers.names()
for ((name, value) in provider.headers) {
if (!headersNames.contains(name)) {
requestBuilder.addHeader(name, value)
}
.build()

}
// 构建请求,发起请求
request = requestBuilder.build()
var response = chain.proceed(request)

if (response.code == HttpURLConnection.HTTP_UNAUTHORIZED) {
//如果返回了401,则尝试通过provider获取新的token
val accessToken = provider.refreshToken()
Expand Down

0 comments on commit 9dfd652

Please sign in to comment.