java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot获取微信JSDK签名信息

springboot获取微信JSDK签名信息的实现示例

作者:sccd2009

本文介绍了如何在Spring Boot应用中获取微信JSDK的签名信息,包括获取接口URL、参数设置、签名算法和获取签名结果的步骤,具有一定的参考价值,感兴趣的可以了解一下

目标

现实项目的需要,需要获取微信jssdk签名信息

步骤

1.  初始化配置信息

public Map initJSSDKConfigInfo(String shareUrl) throws Exception {
        //String accessToken = this.getAccessToken();
        String jsapiTicket = this.getJsApiTicket();
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String nonceStr = UUID.randomUUID().toString();
        String signature = this.buildJSSDKSignature(jsapiTicket,timestamp,nonceStr,shareUrl);

        Map<String,String> map = new HashMap<String,String>();
        map.put("shareUrl", shareUrl);
        map.put("jsapi_ticket", jsapiTicket);
        map.put("nonceStr", nonceStr);
        map.put("timestamp", timestamp);
        map.put("signature", signature);
        map.put("appid", WeChatConfig.appId);
        return map;
    }

2. ticket信息获取

private String getJsApiTicket() {
        String jsApiTicket =redisCache.getCacheObject(JS_API_TICKET);
        if (!StrUtil.isNotEmpty(jsApiTicket)){
            //获取全局的access_token,唯一票据
            String accessToken = redisCache.getCacheObject(ACCESS_TOKEN_KEY);
            //log.info("getJsApiTicket"+accessToken);
            if (StringUtils.isNotBlank(accessToken)) {
                //获取jsapi_ticket
                String url = WeChatConfig.getTicketUrl+"access_token="+accessToken+"&type=jsapi";
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                //请求
                RestTemplate restTemplate = new RestTemplate();
                HttpEntity<String> request = new HttpEntity<>(headers);
                ResponseEntity<String> responseString = restTemplate.postForEntity(url, request, String.class);
                JSONObject json = JSONObject.parseObject(responseString.getBody());
                jsApiTicket = json.getString("ticket");
                redisCache.setCacheObject(JS_API_TICKET, jsApiTicket, 115, TimeUnit.MINUTES);
            }else {
                //access为空打印
                log.info("accessToken","重新获取");
                this.creatAccessToken();
                accessToken = redisCache.getCacheObject(ACCESS_TOKEN_KEY);
                String url = WeChatConfig.getTicketUrl+"access_token="+accessToken+"&type=jsapi";
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                //请求
                RestTemplate restTemplate = new RestTemplate();
                HttpEntity<String> request = new HttpEntity<>(headers);
                ResponseEntity<String> responseString = restTemplate.postForEntity(url, request, String.class);
                JSONObject json = JSONObject.parseObject(responseString.getBody());
                jsApiTicket = json.getString("ticket");
                redisCache.setCacheObject(JS_API_TICKET, jsApiTicket, 115, TimeUnit.MINUTES);

            }
        }
        log.info("getJsApiTicket jsApiTicket: {}", jsApiTicket);
        return jsApiTicket.trim();
    }

3. 信息处理封装

public static String buildJSSDKSignature(String ticket,String timestamp,String nonceStr ,String url) throws Exception {

        String orderedString = "jsapi_ticket=" + ticket
                + "&noncestr=" + nonceStr + "&timestamp=" + timestamp
                + "&url=" + url;
        return sha1(orderedString);
    }
    public static String sha1(String orderedString) throws Exception {
        String ciphertext = null;
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] digest = md.digest(orderedString.getBytes());
        ciphertext = byteToStr(digest);
        return ciphertext.toLowerCase();
    }
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }

到此这篇关于springboot获取微信JSDK签名信息的实现示例的文章就介绍到这了,更多相关springboot获取微信JSDK签名信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文