首页 > 代码库 > 获取微信签名
获取微信签名
微信开发----配置权限获取签名
url
@POST @Path("getJsApi") public DataCenter<Object> getJsApi(String url){ DataCenter<Object> netData = http://www.mamicode.com/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("×tamp=").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; }
获取微信签名
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。