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

焦作做网站最专业的公司访问wordpress时失败

焦作做网站最专业的公司,访问wordpress时失败,建设公司双网注册,网站开发下载1. 判断本地 token 是否过期解析避免每次都去认证服务器申请新 token。本地缓存 token&#xff0c;并在快到期时才刷新。long now System.currentTimeMillis(); if (cachedToken ! null && now < expiresAtMillis - 60_000L) {return cachedToken; }2. 组装认证请求…

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://www.dtcms.com/a/438073.html

相关文章:

  • K8s学习笔记(十二) volume存储卷
  • 十分钟搭建thinkphp开发框架
  • JVM中的内存区域划分
  • 做网站用小图标在什么网下载电脑如何做网站
  • FFmpeg 全面教程:从安装到高级应用
  • 10月3号
  • QT肝8天15--左侧静态菜单
  • 开源安全工具推荐:afrog- 主要用于 Bug Bounty、Pentest 和 Red Teaming 的安全工具
  • Go中的字符串
  • 为什么要做外贸网站公司邮箱域名是什么
  • 《政企API网关:安全与性能平衡的转型实践》
  • 安卓基础组件026-TabLayout
  • Day03_刷题niuke20251004
  • LeetCode:88.乘积最大子数组
  • 7.Java线程中的重要方法(interrupt、isInterrupted、interrupted)
  • 【深度学习计算机视觉】09:语义分割和数据集
  • Vue3 + Three.js 实现 3D 汽车个性化定制及展示
  • 外贸网站 费用广告公司取名字
  • 金融分析师技能提升路径与学习资源指南
  • MySQL processes, threads, connections的区别
  • 自己做的网站首页变成符号了工程与建设
  • P6374 「StOI-1」树上询问(倍增+LCA)
  • epoll_ctl函数中`sockfd` 和 `ev.data.fd`的疑问解析
  • 做元器件上什么网站做网站公司的排名
  • hadoop-hdfs-secondaryNameNode
  • 每日一个网络知识点:OSI参考模型
  • 怎么在国外网站做推广wordpress企业主题制作视频教程
  • K8s不同工作负载对应LOL里哪位英雄
  • 【探寻C++之旅】第十六章:unordered系列的认识与模拟实现
  • 用terraform 创建一个GKE private cluster