微信jsapi签名算法-java
微信jsapi签名算法-java
秦学强 发表于5个月前
微信jsapi签名算法-java
  • 发表于 5个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0
@RequestMapping("/wechatCheck")
    public void wechatCheck(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String url = request.getParameter("url");
        Properties properties = PropertiesFileUtil.getApplicationProperties();
        String appid = properties.getProperty("weixin.appid");
        String appsecret = properties.getProperty("weixin.appsecret");
        String jsapi_ticket = "";

        String res = HttpRequestUtil.httpsGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appid + "&secret=" + appsecret );
        String access_token = JSONObject.fromObject(res).getString("access_token");
        res = HttpRequestUtil.httpsGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi");
        jsapi_ticket = JSONObject.fromObject(res).getString("ticket");
        String timestamp = String.valueOf(CommonUtil.getCurrTimes());
        String noncestr = CommonUtil.createNonceStr();
        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
        parameters.put("jsapi_ticket", jsapi_ticket);
        parameters.put("timestamp", timestamp);
        parameters.put("noncestr", noncestr);
        parameters.put("url", url);
        parameters.put("signature", createSign(parameters));
        parameters.put("appid",appid);
        JSONObject jsonObject = JSONObject.fromObject(parameters);
        String respStr = jsonObject.toString();
        System.out.println("wechatCheck返回的数据:" + respStr);
        response.getWriter().write(respStr);
    }

    /**  * 微信jsapi签名算法sign  * @param parameters  * @return  */  public static String createSign(SortedMap<Object,Object> parameters){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)  Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
// key = "192006250b4c09247ec02edce69f6a2d";//debugTest // sb.append("key=" + key);  sb.deleteCharAt(sb.length() - 1);
        logger.debug(sb);
        String sign = CommonUtil.SHA1(sb.toString());
        return sign;
    }

 

共有 人打赏支持
秦学强
粉丝 0
博文 69
码字总数 0
×
秦学强
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: