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

令牌获取与认证机制详解

1. 判断本地 token 是否过期

解析

  • 避免每次都去认证服务器申请新 token。

  • 本地缓存 token,并在快到期时才刷新。

    long now = System.currentTimeMillis();
    if (cachedToken != null && now < expiresAtMillis - 60_000L) {return cachedToken;
    }
    

2. 组装认证请求参数与请求头

解析

  • 构造向认证服务器申请 token 所需参数(如 client_id、client_secret)。

  • 头信息声明请求体为 JSON。

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    Map<String, String> params = new HashMap<>();
    params.put("grant_type", "client_credentials");
    params.put("client_id", "your_client_id");
    params.put("client_secret", "your_secret");
    

3. 发送 POST 请求获取令牌

解析

  • 用 RestTemplate 向 token 接口发送请求,获取认证信息。

    HttpEntity<Map<String, String>> entity = new HttpEntity<>(params, headers);
    ResponseEntity<String> resp = restTemplate.postForEntity(tokenUrl, entity, String.class);
    String respJson = resp.getBody();
    

4. 解析令牌信息并缓存

解析

  • 解析 JSON 拿到 access_token、过期时间等关键信息。

  • 本地保存,方便下次使用。

    JSONObject obj = JSON.parseObject(respJson);
    cachedToken = obj.getString("access_token");
    expiresAtMillis = now + obj.getLongValue("expires_in") * 1000;
    return cachedToken;
    

完整示例代码

public synchronized String getToken() {long now = System.currentTimeMillis();if (cachedToken != null && now < expiresAtMillis - 60_000L) {return cachedToken;}// 1. 组装认证参数和请求头HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);Map<String, String> params = new HashMap<>();params.put("grant_type", "client_credentials");params.put("client_id", "your_client_id");params.put("client_secret", "your_secret");// 2. 发起 POST 请求HttpEntity<Map<String, String>> entity = new HttpEntity<>(params, headers);ResponseEntity<String> resp = restTemplate.postForEntity(tokenUrl, entity, String.class);String respJson = resp.getBody();// 3. 解析 access_token 并缓存JSONObject obj = JSON.parseObject(respJson);cachedToken = obj.getString("access_token");expiresAtMillis = now + obj.getLongValue("expires_in") * 1000;return cachedToken;
}


文章转载自:
http://brinkman.gbfuy28.cn
http://acceptive.gbfuy28.cn
http://chillness.gbfuy28.cn
http://aut.gbfuy28.cn
http://carambola.gbfuy28.cn
http://berliozian.gbfuy28.cn
http://airfield.gbfuy28.cn
http://chase.gbfuy28.cn
http://alveoloplasty.gbfuy28.cn
http://askari.gbfuy28.cn
http://barnstormer.gbfuy28.cn
http://buckjumper.gbfuy28.cn
http://agriculturalist.gbfuy28.cn
http://anosmia.gbfuy28.cn
http://carbanion.gbfuy28.cn
http://ballooning.gbfuy28.cn
http://bland.gbfuy28.cn
http://bascule.gbfuy28.cn
http://aswoon.gbfuy28.cn
http://brookite.gbfuy28.cn
http://automobile.gbfuy28.cn
http://beeswing.gbfuy28.cn
http://cardiography.gbfuy28.cn
http://betimes.gbfuy28.cn
http://castanets.gbfuy28.cn
http://asu.gbfuy28.cn
http://anapurna.gbfuy28.cn
http://bookcraft.gbfuy28.cn
http://allophane.gbfuy28.cn
http://celestine.gbfuy28.cn
http://www.dtcms.com/a/280820.html

相关文章:

  • 关键点检测数据格式转换(.JSON转TXT)
  • 【超分论文精读】——LightBSR(ICCV2025)
  • 梳理Bean的创建流程
  • mongoDB的CRUD
  • Visual Studio 现已支持新的、更简洁的解决方案文件(slnx)格式
  • 云服务器如何管理数据库(MySQL/MongoDB)?
  • 基于STM32G431无刷电机驱动FOC软硬件学习
  • iOS高级开发工程师面试——常见第三方框架架构设计
  • C++学习笔记五
  • Gemma-3n-E4B-it本地部署教程:谷歌开源轻量级多模态大模型,碾压 17B 级同类模型!
  • SHAP 值的数值尺度
  • Conda 核心命令快速查阅表
  • 技术演进中的开发沉思-35 MFC系列:消息映射与命令
  • Keepalived双机热备
  • 网络安全职业指南:探索网络安全领域的各种角色
  • 003大模型基础知识
  • React 实现老虎机滚动动画效果实例
  • AutojsPro 9.3.11 简单hook
  • Pixel Reasoner:通过好奇心驱动的强化学习激励像素空间推理
  • 简单2步配置CadenceSkill开发编辑器,支持关键字高亮
  • [AI-video] Web UI | Streamlit(py to web) | 应用配置config.toml
  • (李宏毅)deep learning(五)--learning rate
  • 从底层技术到产业落地:优秘企业智脑的 AI 革命路径解析
  • NAT的核心原理以及配置
  • CCF-GESP 等级考试 2025年6月认证Python四级真题解析
  • RDMA over RoCE V2设计2:系统框架设计考虑
  • Datawhale AI夏令营 机器学习2.1
  • 详解低速容错CAN(附与高速CAN对比表)
  • RabbitMQ第三章(企业级MQ应用方案)
  • 基于uniapp+vue3封装的一个日期选择组件