Map缓存token

Map缓存Token 记录过期时间

java 复制代码
public String getToken() {
        AccessTokenSingleton atsl = AccessTokenSingleton.getInstance();
        Map<String, String> map = atsl.getMap();
        String accessToken = map.get("access_token");
        String time = map.get("time");
        Long nowDate = new Date().getTime();
        if (null != accessToken && null != time && nowDate - Long.parseLong(time) < 7200000) {
            log.error("look time:" + time + "; [北森] access_token" + accessToken);
            // 从缓存中读取accessToken数据
            return accessToken;
        } else {
            System.out.println("到期");
            // 获取token
            String tokenUrl = "https://get/token";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("grant_type", "client_credentials");
            params.put("app_key", "7B61AE4");
            params.put("app_secret", "60537D07A1D945D7A70DFF4D2");
            String sendJson = BizJsonUtils.toJson(params);
            HttpRequest post = HttpUtil.createPost(tokenUrl);
            post.body(sendJson);
            
            try (HttpResponse execute = post.execute()) {
              
                if (!execute.isOk()) {
                    throw new BizException("接口异常:" + execute);
                }
                String body = execute.body();
                JSONObject resJSON = JSONUtil.parseObj(body);
                Object access_token = resJSON.get("access_token");
                String errorCode = resJSON.get("error_code")==null?"":resJSON.get("error_code").toString();
                
                if (CFCUtils.isEmpty(access_token)||errorCode.equals("500010")) {
                    String msg = resJSON.get("error_description") == null ? "" : String.valueOf(resJSON.get("error_description"));
                    throw new BizException(msg);
                }
                Map<String, Object> responseMap = new HashMap<>();
                responseMap.put("access_token", access_token);
               
                accessToken = String.valueOf(responseMap.get("access_token"));
               
                map.put("time", String.valueOf(nowDate));
                map.put("access_token", accessToken);
            } catch (Exception e) {
                log.error("接口异常:{}", e.getMessage());
                throw new BizException(e.getMessage());
            }

            return accessToken;
        }

    }

单例设计模式AccessTokenSingleton 缓存token

java 复制代码
 static class AccessTokenSingleton {
        // 缓存accessToken 和 过期时间的 map
        private Map<String, String> map = new HashMap<String, String>();

        private AccessTokenSingleton() {
        }

        private static AccessTokenSingleton single = null;

        public static AccessTokenSingleton getInstance() {
            if (null == single) {
                single = new AccessTokenSingleton();
            }
            return single;
        }

        public Map<String, String> getMap() {
            return map;
        }

        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    }
相关推荐
Fireworkitte4 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083164 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT5 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.5 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超5 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice5 小时前
对象的finalization机制Test
java·开发语言·jvm
望获linux6 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook7 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ7 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
summer夏1237 小时前
2025.07 做什么
java·android studio