java微信公众平台开发

简介

从java后端开发转向web前端开发之后,很少关注及编写后端代码,最近由于公司的一款H5小游戏要求做一个涉及微信公众号分享的功能,由于功能比较简单,又不想去麻烦后端开发的同事,而且本人以前做个不少与微信公众号相关的产品,也没有采用啥框架直接selvet走起

效果如图所示

Screenshot_2017-10-15-17-36-22-228_com.tencent.gif

轻车熟路,代码撸起来。

环境搭建

由于涉及到微信公众平台api接口的调用,建议可以下载一个代理,用来调试微信公众号api接口,我推荐大家使用ngrok,ngrok服务器搭建步骤请参考 http://www.jianshu.com/p/b254547b9fe5

公众号的配置

建议采用微信测试的公众号调用接口地址如下:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

Screenshot_2017-10-15-17-45-35-475_com.tencent.mm.png

具体的配置

image.png

工程采用selvet依赖的jar包如下

image.png
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
    }
}

总结

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容