简介
从java后端开发转向web前端开发之后,很少关注及编写后端代码,最近由于公司的一款H5小游戏要求做一个涉及微信公众号分享的功能,由于功能比较简单,又不想去麻烦后端开发的同事,而且本人以前做个不少与微信公众号相关的产品,也没有采用啥框架直接selvet走起
效果如图所示
轻车熟路,代码撸起来。
环境搭建
由于涉及到微信公众平台api接口的调用,建议可以下载一个代理,用来调试微信公众号api接口,我推荐大家使用ngrok,ngrok服务器搭建步骤请参考 http://www.jianshu.com/p/b254547b9fe5
公众号的配置
建议采用微信测试的公众号调用接口地址如下:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
具体的配置
工程采用selvet依赖的jar包如下
Java 信任所有SSL证书
package com.hj.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* Java 信任所有SSL证书
* @author weijb
*
*/
public class SslUtils {
public static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
}
/**
* 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
*
* @throws Exception
*/
public static void ignoreSsl() throws Exception {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
java微信公众号工具类
package com.hj.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.hj.pojo.weixin.req.Article;
import com.hj.pojo.weixin.req.BaseMessage;
import com.hj.pojo.weixin.req.NewsMessage;
import com.hj.pojo.weixin.req.TextMessage;
import com.hj.share.Share;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import net.sf.json.JSONObject;
/**
* 微信工具类
*
* @author weijb
*
*/
public class WeixinUtil {
public static final String RESP_MESSAGE_TYPE_TEXT = "text"; // 返回消息类型:文本
public static final String RESP_MESSAGE_TYPE_NEWS = "news"; // 返回消息类型:图文
public static final String REQ_MESSAGE_TYPE_TEXT = "text"; // 请求消息类型:文本
public static final String REQ_MESSAGE_TYPE_IMAGE = "image"; // 请求消息类型:图片
public static final String REQ_MESSAGE_TYPE_EVENT = "event"; // 请求消息类型:推送
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe"; // 事件类型:subscribe(订阅)
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe"; // 事件类型:unsubscribe(取消订阅)
public static final String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
public final static String js_api_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
public static final String SET_CUSTOMMENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
/* 申明并初始化一个日志对象 */
public static final Logger log = Logger.getLogger(WeixinUtil.class);
/**
* 扩展xstream,使其支持CDATA块
*
* @date 2013-05-19
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
@SuppressWarnings("unchecked")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
/**
* 获取access token
*
* @param appid
* @param secret
* @return 返回accesstoken码
*/
public static String getAccessToken(String appid, String secret) {
String accessToken = "";
try {
if (null != appid && !"".equals(appid) && null != secret && !"".equals(secret)) {
String url = GET_TOKEN + "&appid=" + appid + "&secret=" + secret;
String result = httpClientGet(url);
System.out.println(result);
if (null != result && !"".equals(result)) {
JSONObject jo = JSONObject.fromObject(result);
// 判断数据是否正确
if (!jo.containsKey("errcode")) {
accessToken = jo.get("access_token").toString();
} else {
Share.errorMessage = jo.get("errmsg").toString();
}
}
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
/**
* 发送get请求
*
* @param urls
* @return
* @throws Exception
*/
public static String httpClientGet(String urls) throws Exception {
StringBuffer sb = new StringBuffer();
URL url = new URL(urls);
// trustAllHttpsCertificates();
SslUtils.trustAllHttpsCertificates();
SslUtils.ignoreSsl();
// HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
// 连接超时时间
httpsConn.setConnectTimeout(10000);
// 读取数据超时时间
httpsConn.setReadTimeout(10000);
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
int respInt = insr.read();
while (respInt != -1) {
sb.append((char) respInt);
respInt = insr.read();
}
return sb.toString();
}
public static String httpClientPost(String urls, String data) {
try {
StringBuffer sb = new StringBuffer();
URL url = new URL(urls);
SslUtils.trustAllHttpsCertificates();
SslUtils.ignoreSsl();
HttpsURLConnection httpsConn = (HttpsURLConnection) url
.openConnection();
httpsConn.setUseCaches(false);
httpsConn.setDoOutput(true);
httpsConn.setDoInput(true);
httpsConn.setFollowRedirects(true);
httpsConn.setInstanceFollowRedirects(true);
httpsConn.setRequestMethod("POST");
OutputStreamWriter post = new OutputStreamWriter(
httpsConn.getOutputStream(), "UTF-8");
post.write(data);
post.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
httpsConn.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
post.close();
in.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
Map<String, String> map = new HashMap<String, String>(); // 将解析结果存储在HashMap中
InputStream inputStream = request.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
SAXReader reader = new SAXReader(); // 解析请求
Document document = reader.read(in);
Element root = document.getRootElement();
List<Element> elementList = root.elements();
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
inputStream.close();
inputStream = null;
return map;
}
/**
* 文本消息对象转换成xml
*
* @param textMessage
* 文本消息对象
* @return xml
*/
public static String textMessageToXml(TextMessage textMessage) {
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
/**
* 图文消息对象转换成xml
*
* @param newsMessage
* 图文消息对象
* @return xml
*/
public static String newsMessageToXml(NewsMessage newsMessage) {
xstream.alias("xml", newsMessage.getClass());
xstream.alias("item", new Article().getClass());
return xstream.toXML(newsMessage);
}
/**
*
* @param requestMap
* @param reply_model
* @return
*/
public static BaseMessage setBaseMessage(Map<String, String> requestMap, int reply_model) {
BaseMessage baseMessage = null;
try {
String fromUserName = requestMap.get("FromUserName");
String toUserName = requestMap.get("ToUserName");
// String msgType = requestMap.get("MsgType");
if (reply_model == 2) {
baseMessage = new TextMessage();
baseMessage.setMsgType(RESP_MESSAGE_TYPE_TEXT);
} else if (reply_model == 3) {
baseMessage = new NewsMessage();
baseMessage.setMsgType(RESP_MESSAGE_TYPE_NEWS);
}
baseMessage.setToUserName(fromUserName);
baseMessage.setFromUserName(toUserName);
baseMessage.setCreateTime(new Date().getTime());
} catch (Exception e) {
e.printStackTrace();
}
return baseMessage;
}
/**
*
* @param access_token 调用微信接口的秘钥
* @param jsonstr 菜单数据
* @return
*/
public static String setCustomMenu(String access_token, String jsonstr) {
String result = "";
String url = SET_CUSTOMMENU + access_token;
result = httpClientPost(url, jsonstr);
return result;
}
}
分享的控制器如下
package com.hj.share;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.hj.pojo.JsapiTicket;
import com.hj.pojo.Sign;
import com.hj.pojo.weixin.menu.TestMenu;
import com.hj.util.ProperHelper;
import com.hj.util.WeixinUtil;
/**
* 分享接口action
*
* @author 魏金波
*
*/
public class Share extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String access_token;
// 主动调用:请求token的时间
public static Long access_token_date;
// jsapi_ticket 微信js安全域凭证
public static String jsapi_ticket;
public static String appId = ProperHelper.getValue("appId");//
public static String appSecret = ProperHelper.getValue("appSecret");//
/* 前端jsapi调用的地址 获取页面路径(前端获取时采用location.href.split('#')[0]获取url) */
public static String url = ProperHelper.getValue("url");
public static String errorMessage = "";
/**
* js签名验证
*
* @param request
* @param response
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String key = request.getParameter("token");
/* 1.判断access_token是否存在 ,如果存在当前的时间 - access_token_date 是否在两个小时之外 */
try {
JSONObject json = new JSONObject();
if (key != null) {
if (key.equals("######")) {
access_token = null;
json.put("access_token_error", errorMessage);
json.put("info", "token remove");
} else if (key.equals("######")) {
if (access_token == null) {
Share.getJsapiTicket(appId, appSecret);
access_token_date = System.currentTimeMillis();
}
String result = WeixinUtil.setCustomMenu(access_token, TestMenu.getMenu());
json = JSONObject.fromObject(result);
} else if (key.equals("########")) {
json.put("success", true);
json.put("info", access_token);
}
}
out.print(json);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
}
/*
* 微信推送消息的入口 (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
* , javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
url = request.getParameter("url").isEmpty() ? "" : request.getParameter("url");
PrintWriter out = response.getWriter();
try {
/* 1.判断access_token是否存在 ,如果存在当前的时间 - access_token_date 是否在两个小时之外 */
if (access_token == null) {
access_token_date = System.currentTimeMillis();
Share.getJsapiTicket(appId, appSecret);
} else if (System.currentTimeMillis() - access_token_date > 1.5 * 60 * 60 * 1000) {
access_token_date = System.currentTimeMillis();
Share.getJsapiTicket(appId, appSecret);
}
Map<String, String> ret = Sign.sign(jsapi_ticket, url);
ret.put("appId", appId);
JSONObject json = JSONObject.fromObject(ret);
out.print(json);
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
}
/**
* 获取jsapi的票据
*
* @param appid
* @param appsecret
* @return
*/
public static JsapiTicket getJsapiTicket(String appid, String appsecret) {
JsapiTicket ticket = null;
try {
access_token = WeixinUtil.getAccessToken(appid, appsecret);
ticket = new JsapiTicket();
String requestUrl = WeixinUtil.js_api_ticket_url.replace("ACCESS_TOKEN", access_token);
JSONObject jsonObject;
jsonObject = JSONObject.fromObject(WeixinUtil.httpClientGet(requestUrl));
if (jsonObject.getString("errcode").equals("0")) {
ticket.setTicket(jsonObject.getString("ticket"));
ticket.setExpiresIn(jsonObject.getString("expires_in"));
jsapi_ticket = ticket.getTicket();
WeixinUtil.log.info("有效期:" + ticket.getExpiresIn());
} else {
ticket.setTicket(jsonObject.getString("errcode"));
jsapi_ticket = ticket.getTicket();
WeixinUtil.log.error("获取失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ticket;
}
/**
* Constructor of the object.
*/
public Share() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}