zoukankan      html  css  js  c++  java
  • 获取微信签名

    微信开发----配置权限获取签名

    url

    @POST
        @Path("getJsApi")
        public DataCenter<Object> getJsApi(String url){
            DataCenter<Object> netData = new DataCenter<Object>();
            String times = Long.toString(System.currentTimeMillis()/1000);
            String nonceStr = PropertiesUtil.get("weChat.nonceStr");
            String jsapi = WeChatUtil.getJSSignature(nonceStr,times,url);
            int i =jsapi.lastIndexOf("@");
            String signature = jsapi.substring(0, i);
            String timeOut = jsapi.substring(i+1, jsapi.length());
            Map<String,Object> map = new HashMap<>();
            map.put("appId", Constant.WECHAT_APPID);
            map.put("signature", signature);
            map.put("timeOut", timeOut);
            map.put("timestamp", times);
            map.put("nonceStr", nonceStr);
            netData.setData(map);
            return netData;
        }

    获取js签名

     /** 
         * 获得js授权的签名信息 
         * @throws UnsupportedEncodingException 
         *  
         */  
        public static String getJSSignature(String noncestr, String timestamp, String url){  
            StringBuilder content = new StringBuilder();  
            content.append("jsapi_ticket=").append(getJsapiTicket());
            content.append("&noncestr=").append(noncestr);
            content.append("&timestamp=").append(timestamp);
            content.append("&url=").append(url);
            MessageDigest md = null;  
            String tmpStr = null;  
            try(Formatter formatter = new Formatter()){
                md = MessageDigest.getInstance("SHA-1");  
                // 2.将三个参数字符串拼接成一个字符串进行sha1加密
                md.reset();
                md.update(content.toString().getBytes("UTF-8"));
                for (byte b : md.digest()) {
                    formatter.format("%02x", b);
                }
                long tims = JspaiTicket_access - (System.currentTimeMillis()/1000) - 120;
                tmpStr = formatter.toString().concat("@").concat(Long.toString(tims));
            } catch (Exception e) {  
                e.printStackTrace();  
            }
            content = null;
            logger.debug("----------------------分享获取机密串 :【{}】",tmpStr);
            // 3.将sha1加密后的字符串返回
            return tmpStr;  
        }

     

     获取jsapiTickt 和accessToken  (为保证有效性,可采用redis存储/或者采用Map)

    /**
         * 获取 access_token
         * 代码注解见 getJsapiTicket()  本类
         * 两方法类似
         * @return
         */
        public static String getAccessToken(){
            String relust = null;
            long now = AccessToken_access - System.currentTimeMillis();
            if(AccessToken == null || "".equals(AccessToken) || now <= 0){
                try(Jedis jesis = JedisUtil.getJedisConnection(Constant.TICKET_DB)){
                    // 先取redis ,有值则直接返回 
                    long times = jesis.ttl(WeChatUtil.WECHAT_ACCESSTOKEN_LOCKE);
                    if(times > 300){
                        relust = jesis.get(WECHAT_ACCESSTOKEN_KEY);
                        // 设定本地计时比redis 慢60 
                        AccessToken_access = System.currentTimeMillis() + (times - 60) * 1000;
                    }else{
                        String lock = jesis.set(WECHAT_ACCESSTOKEN_LOCKE, "1" ,"nx", "ex",30);
                        if("OK".equals(lock)){
                            relust = HttpConnectionUtil.getString(ACCESSTOKEN_API.replace("APPID", WECHAT_APPID).replace("APPSECRET", WECHAT_SECRETKEY));
                            JSONObject json = JSONObject.parseObject(relust);
                            relust = json.getString("access_token");
                            if(relust!=null && !"".equals(relust)){
                                jesis.del(WECHAT_ACCESSTOKEN_KEY);
                                jesis.set(WECHAT_ACCESSTOKEN_KEY, relust ,"nx", "ex",7150);
                                AccessToken = relust;
                                AccessToken_access = System.currentTimeMillis() + ((7200-360) * 1000);
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error("--------------------WECHAT_ACCESSTOKEN 获取异常---------------------");
                    return null;
                }
            } 
            logger.debug("--------------------WECHAT_ACCESSTOKEN 获取成功:【{}】---------------------",AccessToken);
            return AccessToken;
        }
        
        /**
         * 获取 jsapi_ticket
         * @return
         */
        public static String getJsapiTicket(){
            String relust = null;
            long now = JspaiTicket_access - System.currentTimeMillis();
            // 判断 ticket 时效性 ,判断 ticket 本地是否有 
            if(JspaiTicket == null || "".equals(JspaiTicket) || now <= 0){
                // 本地没有,则去分布式缓存中取
                try(Jedis jesis = JedisUtil.getJedisConnection(Constant.TICKET_DB)){
                    // 先取redis ,有值则直接返回 
                    long times = jesis.ttl(WeChatUtil.WECHAT_JSAPITICKET_LOCKE);
                    if(times > 300){
                        relust = jesis.get(WECHAT_JSAPITICKET_KEY);
                        // 设定本地计时比redis 慢60 
                        JspaiTicket_access = System.currentTimeMillis() + (times - 60) * 1000;
                    }else{
                        // jsapi 乐观锁 30秒有效
                        String lock = jesis.set(WECHAT_JSAPITICKET_LOCKE, "1" ,"nx", "ex",30);
                        if("OK".equals(lock)){
                            //得锁的线程,进入刷新 jsapi ticket
                            relust = HttpConnectionUtil.getString(WECHAT_GET_JSAPI_API.replace("ACCESS_TOKEN", getAccessToken()));
                            relust = JSONObject.parseObject(relust).getString("ticket");
                            if(relust!=null && !"".equals(relust)){
                                // 刷新 ticket 后保存到本节点,减少 redis 的访问次数
                                jesis.del(WECHAT_JSAPITICKET_KEY);
                                jesis.set(WECHAT_JSAPITICKET_KEY, relust ,"nx", "ex",7150); 
                                JspaiTicket = relust;
                                JspaiTicket_access = System.currentTimeMillis() + ((7200-360) * 1000);
                            }
                        }else{
                            // 没有拿到锁,30秒之后再来的线程从新初始化
                            JspaiTicket_access = JspaiTicket_access + 30;
                        }
                    }
                } catch (Exception e) {
                    logger.error("-------------------- WECHAT_JSAPITICKET 获取异常 ---------------------");
                    return null;
                }
            }
            logger.debug("--------------------WECHAT_JSAPITICKET 获取成功:【{}】---------------------",JspaiTicket);
            return JspaiTicket;
        }
  • 相关阅读:
    [解题报告]256 Quirksome Squares
    [解题报告]369 Combinations
    [解题报告]10696 f91
    [解题报告]483 Word Scramble
    [解题报告]10041 Vito's Family
    [解题报告]686 Goldbach's Conjecture (II)
    [解题报告]151 Power Crisis
    [解题报告]10079 Pizza Cutting
    [解题报告]495 Fibonacci Freeze
    [解题报告]541 Error Correction
  • 原文地址:https://www.cnblogs.com/zhshto/p/6474116.html
Copyright © 2011-2022 走看看