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;
        }
    }
相关推荐
Predestination王瀞潞3 小时前
Java EE开发技术(Servlet整合JDBC银行管理系统-上)
java·servlet·java-ee·jdbc
寻星探路3 小时前
Java EE初阶启程记13---JUC(java.util.concurrent) 的常见类
java·开发语言·java-ee
怪兽20144 小时前
什么是 Redis?
java·数据库·redis·缓存·面试
Gu_yyqx4 小时前
Java 队列
java
落日漫游4 小时前
数据结构笔试核心考点
java·开发语言·算法
疯狂吧小飞牛5 小时前
Lua C API 中的注册表介绍
java·c语言·lua
kyle~5 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
Hello.Reader5 小时前
Flink 受管状态的自定义序列化原理、实践与可演进设计
java·网络·flink
让我上个超影吧5 小时前
设计模式【工厂模式和策略模式】
java·设计模式·策略模式
fs哆哆6 小时前
在VB.NET中,有没有 ?.这个运算符
java·开发语言·.net