1 开发条件
1 申请一个微信公众号,并确认在开发–接口权限中拥有分享功能的权限。
2 公众号设置–功能设置:在JS接口安全域名中添加安全域名,这个安全域名不是url,只需添加一级域名即可。
3 开发,基本配置中,需要获得开发者id,开发者密码,ip白名单,ip白名单是服务器的ip。
4 获得jsapi_ticket:
先获取access_token:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
接口调用获取:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数 说明
grant_type 获取access_token填写client_credential
appid 第三方用户唯一凭证
secret 第三方用户唯一凭证密钥,即appsecret
获取jsapi_ticket https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
5 signature获取:
jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW×tamp=1414587457&url=http://mp.weixin.qq.com?params=value
2 在后台使用动态url方式获取signature
前台:
$(document).ready(function(){ initPage(); }); function initPage() { //alert(window.location.href);/***用于获得当前连接url用**/ /***用户点击分享到微信圈后加载接口接口*******/ $.post("/business/businessAction.do?method=getSharData",{"url":location.href.split('#')[0]},function(data,status){ data=eval("("+data+")"); console.log(data); wx.config({ debug: false, appId: 'wx071f25226923fc00', timestamp:data.timestamp, nonceStr:data.nonceStr, signature:data.signature, jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'hideOptionMenu', ] }); wx.ready(function(){ wx.hideOptionMenu();/***隐藏分享菜单****/ }); }); };
后台:
import java.util.UUID; import java.util.Map; import java.util.HashMap; import java.util.Formatter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.io.UnsupportedEncodingException; /** *用来获得微信验证的所有参数 */ public class Sign { public static void main(String[] args) { String jsapi_ticket = "bxLdikRXVbTPdHSM05e5u7pfX712TBiu6cLRUR0dNkywjZCMepIQeCTufMFPO6ARKcFQ1B_SkwMCD1MlAS-96Q"; // 注意 URL 一定要动态获取,不能 hardcode String url = "http://vip.cioe.cn"; Map<String, String> ret = sign(jsapi_ticket, url); for (Map.Entry entry : ret.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } }; public static Map<String, String> sign(String jsapi_ticket, String url) { Map<String, String> ret = new HashMap<String, String>(); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String string1; String signature = ""; //注意这里参数名必须全部小写,且必须有序 string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url; System.out.println(string1); try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(string1.getBytes("UTF-8")); signature = byteToHex(crypt.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ret.put("url", url); ret.put("jsapi_ticket", jsapi_ticket); ret.put("nonceStr", nonce_str); ret.put("timestamp", timestamp); ret.put("signature", signature); return ret; } private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } private static String create_nonce_str() { return UUID.randomUUID().toString(); } private static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } }
3 改为接口进行访问
@ApiOperation(value = "2.获取企业微信签名", notes = "2.获取企业微信签名") @ApiImplicitParams({ @ApiImplicitParam(name = "url", paramType = "query", value = "url" ,required = true) }) @GetMapping("/getSign") public ResponseEntity<Map<String, String>> getSign(String url) throws ApiException { AccessTokenVO accessToken = WeComUtil.getAccessToken(); String access_token = accessToken.getAccess_token(); ResponseEntity<String> forEntity = restTemplate.getForEntity("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + access_token, String.class); String body = forEntity.getBody(); com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(body); String ticket = jsonObject.getString("ticket"); Map<String, String> sign = Sign.sign(ticket, url); return new ResponseEntity<>(sign,HttpStatus.OK); } @ApiOperation(value = "3.获取微信公众号签名", notes = "3.获取微信公众号签名") @ApiImplicitParams({ @ApiImplicitParam(name = "url", paramType = "query", value = "url" ,required = true) }) @GetMapping("/getWechatMpSign") public ResponseEntity<Map<String, String>> getWechatMpSign(String url) throws ApiException { AccessTokenVO mpAccessToken = WeChatMpUtil.getMpAccessToken(); String access_token = mpAccessToken.getAccess_token(); ResponseEntity<String> forEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi", String.class); String body = forEntity.getBody(); com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(body); String ticket = jsonObject.getString("ticket"); Map<String, String> sign = Sign.sign(ticket, url); return new ResponseEntity<>(sign,HttpStatus.OK); }