第一步:先登录微信公众平台进行设置JS接口安全域名("域名不能包含http://")。
如图:
第二步:配置config接口权限验证
/**
* 获取access_token
* @return
*/
public static String getaccess_token(String url){
String tokenurl=url+"l&appid="+WeiXinUtil.appID
+"&secret="+WeiXinUtil.appsecret;
String resultStr="";
try {
resultStr=HttpUploadFile.getUrlCon(tokenurl);
JSONObject objec=JSONObject.parseObject(resultStr);
System.out.println("返回结果>>>>>>>>>:"+resultStr);
if(!StringUtils.isEmpty(objec.getString("access_token"))){
resultStr=objec.getString("access_token");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取token异常");
}
System.out.println("获取token值为>>>>>>>>:"+resultStr);
return resultStr;
}
try {
resultStr=HttpUploadFile.getUrlCon(url);
JSONObject objec=JSONObject.parseObject(resultStr);
System.out.println("返回结果>>>>>>>>>:"+resultStr);
if(objec!=null){
resultStr=objec.getString("ticket");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取ticket异常");
}
然后验证签名
@ResponseBody
public Map share(String url) throws Exception {
Map map = new HashMap();
String jsapi_ticket = WeiXinInterface.getjsapiTicket();//jsapi_ticket
String timestamp = Long.toString(System.currentTimeMillis() / 1000);//时间戳
String nonceStr = UUID.randomUUID().toString();
String signature = SignUtil.getSignature(
jsapi_ticket, nonceStr, timestamp,
url);//验证签名
map.put("url", url);
map.put("jsapi_ticket", jsapi_ticket);
map.put("nonceStr", nonceStr);
map.put("timestamp", timestamp);
map.put("signature", signature);
map.put("appid",WeiXinUtil.appID);
return map;
}
SginUtil工具类
package com.hlkj.utils;
import java.security.MessageDigest;
/**
* 微信签名工具类
* @author 楪祈
*
*/
public class SignUtil {
/**
* 获得分享链接的签名。
* @param ticket
* @param nonceStr
* @param timeStamp
* @param url
* @return
* @throws Exception
*/
public static String getSignature(String ticket, String nonceStr, String timeStamp, String url) throws Exception {
String sKey = "jsapi_ticket=" + ticket
+ "&noncestr=" + nonceStr + "×tamp=" + timeStamp
+ "&url=" + url;
System.out.println(sKey);
return getSignature(sKey);
}
/**
* 验证签名。
*
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static String getSignature(String sKey) throws Exception {
String ciphertext = null;
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(sKey.toString().getBytes());
ciphertext = byteToStr(digest);
return ciphertext.toLowerCase();
}
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 将字节转换为十六进制字符串
*
* @param mByte
* @return
*/
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;
}
}
第三步:需要进行分享功能的页面编写(需要导入微信提供的js)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/static/";
%>
微信分享页面
暂时没有内容
在开发中遇到 config:invalid url domain的原因