OkHttp源码学习之Authenticator

OkHttp 中,Authenticator 是一个用于处理 HTTP 认证(Authentication)的接口,主要用于在服务端要求身份验证时自动提供凭据(如用户名和密码)。

Authenticator 的作用

  1. 处理 HTTP 401(Unauthorized)和 407(Proxy Authentication Required)状态码

    当服务器或代理返回这些状态码时,Authenticator 会被调用以提供认证信息。

  2. 自动重试请求
    OkHttp 使用 Authenticator 生成包含认证头的请求,并自动重试。这简化了开发流程,无需手动处理这些情况。

  3. 支持多种认证方式

    通过自定义实现,可以支持 BasicBearer 或其他定制的认证方式。

Authenticator 的实现

Authenticator 是一个函数式接口,定义了以下方法:

less 复制代码
java
复制代码
public interface Authenticator {
    Request authenticate(@Nullable Route route, Response response) throws IOException;
}

参数说明

  • route:提供网络请求的路由信息(如目标主机和端口)。
  • response:包含服务端的响应信息,通常包含 WWW-AuthenticateProxy-Authenticate 头,用于指示所需的认证方式。

返回值

  • 返回一个新的 Request 对象,其中包含了认证头(AuthorizationProxy-Authorization)。
  • 返回 null 表示放弃认证,OkHttp 不会重试请求。

默认 Authenticator

  • Authenticator.NONE :表示不处理认证,返回 null

常见场景与实现

1. Basic Authentication

kotlin 复制代码
val basicAuthenticator = Authenticator { _, response ->
    val credentials = Credentials.basic("username", "password")
    response.request.newBuilder()
        .header("Authorization", credentials)
        .build()
}

val client = OkHttpClient.Builder()
    .authenticator(basicAuthenticator)
    .build()

2. Token Authentication

当需要在请求中添加 Bearer Token 或自定义的认证信息时:

kotlin 复制代码
val tokenAuthenticator = Authenticator { _, response ->
    val token = "your-token-here"  // Replace with logic to retrieve token
    response.request.newBuilder()
        .header("Authorization", "Bearer $token")
        .build()
}

val client = OkHttpClient.Builder()
    .authenticator(tokenAuthenticator)
    .build()

3. Proxy Authentication

针对代理服务器的认证:

kotlin 复制代码
val proxyAuthenticator = Authenticator { _, response ->
    val credentials = Credentials.basic("proxyUser", "proxyPassword")
    response.request.newBuilder()
        .header("Proxy-Authorization", credentials)
        .build()
}

val client = OkHttpClient.Builder()
    .proxyAuthenticator(proxyAuthenticator)
    .build()

重要注意事项

  1. 避免无限循环

    • 如果认证失败,服务端可能继续返回 401407 状态码,这个时候我们需要添加退避策略或者控制重试次数。
    • 确保在 Authenticator 中检查重试次数或响应的请求历史,以避免无限重试。
    kotlin 复制代码
    val limitedRetryAuthenticator = Authenticator { _, response ->
        if (response.request.header("Authorization") != null) {
            // 防止无限重试
            null
        } else {
            val credentials = Credentials.basic("username", "password")
            response.request.newBuilder()
                .header("Authorization", credentials)
                .build()
        }
    }
  2. 线程安全性

    • 如果 Authenticator 需要访问共享资源(如刷新令牌),确保实现是线程安全的。
  3. 避免将敏感信息硬编码

    • 使用安全存储(如 Android 的 Keystore)来存储凭据。
相关推荐
坚持每天敲代码1 小时前
【教程】IDEA中导入springboot-maven工程
java·maven·intellij-idea
袁美丽..1 小时前
Android studio的adb和终端的adb互相抢占端口
android·adb·android studio
CodeCraft Studio1 小时前
国产化PDF处理控件Spire.PDF教程:如何在 Java 中通过模板生成 PDF
java·python·pdf·spire.pdf·java创建pdf·从html创建pdf
阿方.9181 小时前
《数据结构全解析:栈(数组实现)》
java·开发语言·数据结构
YC运维1 小时前
Ansible题目全解析与答案
java·算法·ansible
程序员清风2 小时前
贝壳一面:年轻代回收频率太高,如何定位?
java·后端·面试
鹏多多.2 小时前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
考虑考虑2 小时前
Java实现字节转bcd编码
java·后端·java ee
守城小轩2 小时前
Firefox Android 开发环境搭建全流程(四)
android·firefox·chrome devtools·指纹浏览器·浏览器开发
软件开发-NETKF88882 小时前
JSP到Tomcat特详细教程
java·开发语言·tomcat·jsp·项目运行