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;
        }
    }
相关推荐
神仙别闹19 分钟前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭43 分钟前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫1 小时前
泛型(2)
java
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石1 小时前
12/21java基础
java
李小白661 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp2 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗2 小时前
常用类晨考day15
java