当前位置: 首页 > news >正文

android 网络访问拦截器的编写的几种方式

1、接口访问鉴权原型

一般我们在访问服务器端的时候,比如登录成功了,会有一个信息的返回,一般会有一个auth值,也有可能是一个sessionid,这样我们就可以带着这个值或者id,去访问一些需要鉴权的接口了。

2、客户端调用header鉴权

在android端,我们通常会编写一些拦截器来进行自动加入header,这样就可以去自动加入,不需要手工处理。

3、拦截器的方式

方式一(withContext / runBlocking)

每次请求都同步获取 jsessionId

方式二(CoroutineScope + 缓存)

异步加载并缓存 jsessionId,支持并发控制

方式三(纯 runBlocking)

完全阻塞方式获取 jsessionId

4、三种方式的对比

详细说明:

1. 方式一:使用 runBlocking 获取值


适用场景:如果你能确保该拦截器仅运行在非 UI 线程(如 OkHttp 的后台线程),可以接受。
不推荐原因:
若拦截器在主线程执行(如某些测试或调试场景),会导致卡顿甚至 ANR。
每次请求都重新获取 JSESSIONID,效率低且无必要。

val jsessionId = runCatching {runBlocking(dispatcher) {appDataStore.getJSessionId().firstOrNull()}
}.getOrNull()

2. 方式二:使用 CoroutineScope + 缓存(推荐)


异步加载,不会阻塞主线程。
使用缓存避免频繁读取数据源,提高性能。
支持并发控制(通过 synchronized)。
实现了 Closeable 接口,便于资源释放。

推荐作为生产代码使用,尤其适合需要高性能、稳定性强的 App。

private var cachedJSessionId: String? = null
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)private fun updateJSessionId() {scope.launch {appDataStore.getJSessionId().firstOrNull()?.let {synchronized(updateLock) {cachedJSessionId = it}}}
}

3. 方式三:纯 runBlocking 同步获取


在任何上下文中使用都会导致线程阻塞。
如果 appDataStore.getJSessionId() 是耗时操作(如从磁盘读取),极易造成 ANR。无法应对异常情况。

val jsessionId: String? = runBlocking {appDataStore.getJSessionId().firstOrNull()
}

5、相关代码

 方式一:使用 withContext

/*** 拦截器,用于添加 JSESSIONID 到请求头* 在指定的协程调度器 dispatcher 中,从 appDataStore 获取 JSessionId 的值,并忽略可能的异常,最终将结果赋值给 jsessionId。* 具体解释如下:* runCatching { ... }:捕获代码块中的异常,避免程序崩溃。* runBlocking(dispatcher) { ... }:在指定的协程调度器中启动一个阻塞式协程。* appDataStore.getJSessionId().firstOrNull():获取 JSessionId 的第一个值,若无数据则返回 null。* .getOrNull():从 runCatching 的结果中取出值,若发生异常则返回 null。*/class AuthInterceptor @Inject constructor(private val appDataStore: AppDataStore,private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) : Interceptor {override fun intercept(chain: Interceptor.Chain): Response {val request = chain.request()// 使用 withContext 在 IO 线程获取数据val jsessionId = runCatching {runBlocking(dispatcher) {appDataStore.getJSessionId().firstOrNull()}}.getOrNull()val newRequest = if (jsessionId != null) {val cookie = request.header("Cookie").orEmpty()val newCookie = if (cookie.isEmpty()) {"JSESSIONID=$jsessionId"} else {"$cookie; JSESSIONID=$jsessionId"}request.newBuilder().header("Cookie", newCookie).build()} else {request}return chain.proceed(newRequest)}
}

该 Kotlin 代码定义了一个 AuthInterceptor类,用于在发起网络请求时自动添加 `JSESSIONID` 到 Cookie 中。其逻辑如下:

1. 从 AppDataStore中异步获取 `JSESSIONID`;
2. 若存在 `JSESSIONID`,则将其拼接到请求头的 `Cookie` 字段中;
3. 最终继续执行修改后的请求。

方式二:使用 CoroutineScope

class AuthInterceptor @Inject constructor(private val appDataStore: AppDataStore
) : Interceptor, Closeable {private var cachedJSessionId: String? = nullprivate val updateLock = Any()private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)init {updateJSessionId()}private fun updateJSessionId() {scope.launch {appDataStore.getJSessionId().firstOrNull()?.let {synchronized(updateLock) {cachedJSessionId = it}}}}override fun intercept(chain: Interceptor.Chain): Response {val request = chain.request()var jsessionId = cachedJSessionIdif (jsessionId == null) {synchronized(updateLock) {jsessionId = cachedJSessionIdif (jsessionId == null) {updateJSessionId()return chain.proceed(request)}}}val cookieHeader = request.header("Cookie").orEmpty()val newCookie = if (cookieHeader.isEmpty()) {"JSESSIONID=$jsessionId"} else {"$cookieHeader; JSESSIONID=$jsessionId"}val newRequest = request.newBuilder().header("Cookie", newCookie).build()return chain.proceed(newRequest)}// 实现 Closeable 接口,确保资源释放override fun close() {scope.cancel()}
}

推荐作为生产代码使用,尤其适合需要高性能、稳定性强的 App。

该 Kotlin 代码实现了一个 AuthInterceptor用于在 HTTP 请求中自动添加 `JSESSIONID` Cookie。其功能如下:

1. **构造与初始化**:通过依赖注入获取 AppDataStore用于读取会话 ID,并在初始化时尝试更新会话 ID。
2. **intercept 函数**:
   - 若已有缓存的 `JSESSIONID`,则将其添加到请求头的 Cookie 中;
   - 若没有缓存,则重新加载会话 ID;
   - 使用锁保证线程安全。
3. **updateJSessionId 函数**:使用协程从 AppDataStore 异步加载会话 ID 并缓存。
4. **close 函数**:取消协程作用域,释放资源。

整体作用:为每个请求自动注入最新的 `JSESSIONID`,确保会话状态有效。

方式三:runBlocking阻塞性获取

class AuthInterceptor @Inject constructor(private val appDataStore: AppDataStore
) : Interceptor {override fun intercept(chain: Interceptor.Chain): Response {val request = chain.request()// 在拦截器中同步获取 jsessionid(假设可以阻塞)val jsessionId: String? = runBlocking {appDataStore.getJSessionId().firstOrNull()}// 构建新的请求,添加 cookie(仅当 jsessionId 存在时)val newRequest = request.newBuilder().apply {if (jsessionId != null) {// 获取现有 Cookie 并追加新的 JSESSIONIDval existingCookies = request.header("Cookie")val newCookie = "JSESSIONID=$jsessionId"val finalCookie = if (existingCookies.isNullOrEmpty()) {newCookie} else {"$existingCookies; $newCookie"}addHeader("Cookie", finalCookie)}}.build()return chain.proceed(newRequest)}}

该 Kotlin 代码定义了一个 AuthInterceptor 拦截器,其功能是:
从 AppDataStore 中同步获取 jsessionId(使用 runBlocking 阻塞等待);
若 jsessionId 存在,则将其添加到 HTTP 请求头的 Cookie 字段中;
最终构建新请求并继续拦截器链的执行。
作用:为每个请求自动添加会话标识 JSESSIONID。

6、总结

综合上述几种方式,其实测试下来都是可以的,基本上是没有什么区别,但是如果考虑综合因素,或者多环境下的适应性,还是考虑用使用 CoroutineScope + 缓存(推荐)。

http://www.dtcms.com/a/265749.html

相关文章:

  • React 学习(3)
  • springboot 中使用 websocket
  • PHP:从入门到实践——构建高效Web应用的利器
  • 2011年英语一
  • AlpineLinux安装x11vnc服务端实现远程桌面登录
  • Zephyr RTOS 防止中断影响数据写入
  • cv610将音频chn0配置为g711a,chn1配置为 aac编码,记录
  • ARM SMMUv3故障和错误(五)
  • mac 电脑安装Homebrew来安装npm与node成功后,安装nvm的流程
  • macOS 26正式发布,全新Liquid Glass设计语言亮相
  • join性能问题,distinct和group by性能,备库自增主键问题
  • 微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权
  • 针孔相机模型
  • python学习打卡day59
  • 【轨物洞见】光伏机器人与组件、支架智能化协同白皮书
  • Linux操作系统之文件(二):重定向
  • Android 系统默认的Launcher3,Android 系统的导航栏(通常是屏幕底部)显示的 4 个快捷应用图标,如何替换这4个应用图标为客户想要的。
  • Fiddler中文版抓包工具在后端API调试与Mock中的巧用
  • Treap树
  • thinkphp8接管异常处理类
  • linux系统 weblogic10.3.6(jar) 下载及安装
  • 后端 Maven打包 JAR 文件、前端打包dist文件、通过后端服务访问前端页面、Nginx安装与部署
  • Josn模块的使用
  • MVC 架构设计模式
  • Docker 安装 Redis 哨兵模式
  • 【数据结构】C++的unordered_map/set模拟实现(开散列(哈希桶)作底层)
  • 机器人“触摸”水果成熟度突破:SwishFormer模型与DIGIT视触觉传感器在HelloRobot上的水果检测应用
  • TDSQL如何查出某一列中的逗号数量
  • 从 TCP/IP 协议栈角度深入分析网络文件系统 (NFS)
  • (1)手摸手-学习 Vue3 之 Vite 创建项目