http://jingyan.baidu.com/article/da1091fbcec5da027849d626.html
最近公司在开发微信项目,所以自己也试着申请了个人的订阅服务号,实现了通过微信接收信息转发至java后台解析并回复的消息的简单功能,在还没忘记的时候记录一下,以便日后查阅,并且贡献出代码希望能给大家一个参考。
好首先你要看下面的示例,要事先申请微信公众平台的订阅服务号(个人只能申请这个),地址https://mp.weixin.qq.com ,申请的范例我这里就不讲了,一般根据提示可以自行完成,如果这都完成不了,那只能去度娘翻翻了。
要想让用户发送给公众帐号的消息转发给java后台服务器,首先要 在开发者中心 进行 服务器配置 ,
下图为认证启动后小效果:
你要先进入到 修改配置里面,如下图:
你要填写这几个文本框内的内容,
1.URL 不用解释了,就是微信将用户发来的消息转发到你服务器的请求的地址,我让微信把请求发送到本地服务这样方便调试。
推荐一款反向代理的工具 pagekite.net ,感兴趣的朋友可以去搜索一下。使用相当方便,就是需要python2.7.x环境支持,然后运行下载的一个脚本,输入你的邮箱,然后在输入你要设置的域名前缀,就搞定,下次运行就不用在输入,它影射的是本地80端口,所以你启动服务的时候记得改成80端口就对了,还有这个貌似对于一个邮箱只有31天和5个连接的限制,PS:邮箱这东西都是免费的,你懂的。
2.Token:这个长度符合就行 自己随意
3.EncodingAESKey:点击随机生成 就OK
下面介绍我的代码:
我的开发环境是eclipse+springMvc
"/chat" 是我最终项目的请求Controller URL路径
下面是homecontroller
@Controller @RequestMapping("/*") public class HomeController { private String Token = "123456789abcdef"; @RequestMapping(value = "chat", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody public void liaotian(Model model, HttpServletRequest request, HttpServletResponse response) { System.out.println("进入chat"); boolean isGet = request.getMethod().toLowerCase().equals("get"); if (isGet) { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); System.out.println(signature); System.out.println(timestamp); System.out.println(nonce); System.out.println(echostr); access(request, response); } else { // 进入POST聊天处理 System.out.println("enter post"); try { // 接收消息并返回消息 acceptMessage(request, response); } catch (IOException e) { e.printStackTrace(); } } } /** * 验证URL真实性 * * @author morning * @date 2015年2月17日 上午10:53:07 * @param request * @param response * @return String */ private String access(HttpServletRequest request, HttpServletResponse response) { // 验证URL真实性 System.out.println("进入验证access"); String signature = request.getParameter("signature");// 微信加密签名 String timestamp = request.getParameter("timestamp");// 时间戳 String nonce = request.getParameter("nonce");// 随机数 String echostr = request.getParameter("echostr");// 随机字符串 List<String> params = new ArrayList<String>(); params.add(Token); params.add(timestamp); params.add(nonce); // 1. 将token、timestamp、nonce三个参数进行字典序排序 Collections.sort(params, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); // 2. 将三个参数字符串拼接成一个字符串进行sha1加密 String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2)); if (temp.equals(signature)) { try { response.getWriter().write(echostr); System.out.println("成功返回 echostr:" + echostr); return echostr; } catch (IOException e) { e.printStackTrace(); } } System.out.println("失败 认证"); return null; } private void acceptMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { // 处理接收消息 ServletInputStream in = request.getInputStream(); // 将POST流转换为XStream对象 XStream xs = SerializeXmlUtil.createXstream(); xs.processAnnotations(InputMessage.class); xs.processAnnotations(OutputMessage.class); // 将指定节点下的xml节点数据映射为对象 xs.alias("xml", InputMessage.class); // 将流转换为字符串 StringBuilder xmlMsg = new StringBuilder(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1;) { xmlMsg.append(new String(b, 0, n, "UTF-8")); } // 将xml内容转换为InputMessage对象 InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString()); String servername = inputMsg.getToUserName();// 服务端 String custermname = inputMsg.getFromUserName();// 客户端 long createTime = inputMsg.getCreateTime();// 接收时间 Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000;// 返回时间 // 取得消息类型 String msgType = inputMsg.getMsgType(); // 根据消息类型获取对应的消息内容 if (msgType.equals(MsgType.Text.toString())) { // 文本消息 System.out.println("开发者微信号:" + inputMsg.getToUserName()); System.out.println("发送方帐号:" + inputMsg.getFromUserName()); System.out.println("消息创建时间:" + inputMsg.getCreateTime() + new Date(createTime * 1000l)); System.out.println("消息内容:" + inputMsg.getContent()); System.out.println("消息Id:" + inputMsg.getMsgId()); StringBuffer str = new StringBuffer(); str.append("<xml>"); str.append("<ToUserName><![CDATA[" + custermname + "]]></ToUserName>"); str.append("<FromUserName><![CDATA[" + servername + "]]></FromUserName>"); str.append("<CreateTime>" + returnTime + "</CreateTime>"); str.append("<MsgType><![CDATA[" + msgType + "]]></MsgType>"); str.append("<Content><![CDATA[你说的是:" + inputMsg.getContent() + ",吗?]]></Content>"); str.append("</xml>"); System.out.println(str.toString()); response.getWriter().write(str.toString()); } // 获取并返回多图片消息 if (msgType.equals(MsgType.Image.toString())) { System.out.println("获取多媒体信息"); System.out.println("多媒体文件id:" + inputMsg.getMediaId()); System.out.println("图片链接:" + inputMsg.getPicUrl()); System.out.println("消息id,64位整型:" + inputMsg.getMsgId()); OutputMessage outputMsg = new OutputMessage(); outputMsg.setFromUserName(servername); outputMsg.setToUserName(custermname); outputMsg.setCreateTime(returnTime); outputMsg.setMsgType(msgType); ImageMessage images = new ImageMessage(); images.setMediaId(inputMsg.getMediaId()); outputMsg.setImage(images); System.out.println("xml转换:/n" + xs.toXML(outputMsg)); response.getWriter().write(xs.toXML(outputMsg)); } } }
加密SHA1,此代码是来自网上
/* * 微信公众平台(JAVA) SDK * * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. * http://www.ansitech.com/weixin/sdk/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mor.util; import java.security.MessageDigest; /** * <p> * Title: SHA1算法 * </p> * */ public final class SHA1 { private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Takes the raw bytes from the digest and formats them correct. * * @param bytes * the raw bytes from the digest. * @return the formatted bytes. */ private static String getFormattedText(byte[] bytes) { int len = bytes.length; StringBuilder buf = new StringBuilder(len * 2); // 把密文转换成十六进制的字符串形式 for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); buf.append(HEX_DIGITS[bytes[j] & 0x0f]); } return buf.toString(); } public static String encode(String str) { if (str == null) { return null; } try { MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); messageDigest.update(str.getBytes()); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } }
输入信息实体类 InputMessage
<pre name="code" class="java">/* * 微信公众平台(JAVA) SDK * * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. * http://www.ansitech.com/weixin/sdk/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mor.maven.demo.mavenweb.model; import java.io.Serializable; import com.thoughtworks.xstream.annotations.XStreamAlias; /** * POST的XML数据包转换为消息接受对象 * * <p> * 由于POST的是XML数据包,所以不确定为哪种接受消息,<br/> * 所以直接将所有字段都进行转换,最后根据<tt>MsgType</tt>字段来判断取何种数据 * </p> * */ @XStreamAlias("xml") public class InputMessage implements Serializable { /** * */ private static final long serialVersionUID = 1L; @XStreamAlias("ToUserName") private String ToUserName; @XStreamAlias("FromUserName") private String FromUserName; @XStreamAlias("CreateTime") private Long CreateTime; @XStreamAlias("MsgType") private String MsgType = "text"; @XStreamAlias("MsgId") private Long MsgId; // 文本消息 @XStreamAlias("Content") private String Content; // 图片消息 @XStreamAlias("PicUrl") private String PicUrl; // 位置消息 @XStreamAlias("LocationX") private String LocationX; @XStreamAlias("LocationY") private String LocationY; @XStreamAlias("Scale") private Long Scale; @XStreamAlias("Label") private String Label; // 链接消息 @XStreamAlias("Title") private String Title; @XStreamAlias("Description") private String Description; @XStreamAlias("Url") private String URL; // 语音信息 @XStreamAlias("MediaId") private String MediaId; @XStreamAlias("Format") private String Format; @XStreamAlias("Recognition") private String Recognition; // 事件 @XStreamAlias("Event") private String Event; @XStreamAlias("EventKey") private String EventKey; @XStreamAlias("Ticket") private String Ticket; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public Long getCreateTime() { return CreateTime; } public void setCreateTime(Long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public Long getMsgId() { return MsgId; } public void setMsgId(Long msgId) { MsgId = msgId; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getLocationX() { return LocationX; } public void setLocationX(String locationX) { LocationX = locationX; } public String getLocationY() { return LocationY; } public void setLocationY(String locationY) { LocationY = locationY; } public Long getScale() { return Scale; } public void setScale(Long scale) { Scale = scale; } public String getLabel() { return Label; } public void setLabel(String label) { Label = label; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public String getURL() { return URL; } public void setURL(String uRL) { URL = uRL; } public String getEvent() { return Event; } public void setEvent(String event) { Event = event; } public String getEventKey() { return EventKey; } public void setEventKey(String eventKey) { EventKey = eventKey; } public String getMediaId() { return MediaId; } public void setMediaId(String mediaId) { MediaId = mediaId; } public String getFormat() { return Format; } public void setFormat(String format) { Format = format; } public String getRecognition() { return Recognition; } public void setRecognition(String recognition) { Recognition = recognition; } public String getTicket() { return Ticket; } public void setTicket(String ticket) { Ticket = ticket; } }
为了加入 CDATA 验证创建的@interface类
package com.mor.maven.demo.mavenweb.model; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD }) public @interface XStreamCDATA { }
改写的XStream工具类
package com.mor.util; import java.io.Writer; import java.lang.reflect.Field; import com.mor.maven.demo.mavenweb.model.XStreamCDATA; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamAlias; 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; /** * xml 转换工具类 * * @author morning * @date 2015年2月16日 下午2:42:50 */ public class SerializeXmlUtil { public static XStream createXstream() { return new XStream(new XppDriver() { @Override public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { boolean cdata = false; Class<?> targetClass = null; @Override public void startNode(String name, @SuppressWarnings("rawtypes") Class clazz) { super.startNode(name, clazz); // 业务处理,对于用XStreamCDATA标记的Field,需要加上CDATA标签 if (!name.equals("xml")) { cdata = needCDATA(targetClass, name); } else { targetClass = clazz; } } @Override protected void writeText(QuickWriter writer, String text) { if (cdata) { writer.write("<![CDATA["); writer.write(text); writer.write("]]>"); } else { writer.write(text); } } }; } }); } private static boolean needCDATA(Class<?> targetClass, String fieldAlias) { boolean cdata = false; // first, scan self cdata = existsCDATA(targetClass, fieldAlias); if (cdata) return cdata; // if cdata is false, scan supperClass until java.lang.Object Class<?> superClass = targetClass.getSuperclass(); while (!superClass.equals(Object.class)) { cdata = existsCDATA(superClass, fieldAlias); if (cdata) return cdata; superClass = superClass.getClass().getSuperclass(); } return false; } private static boolean existsCDATA(Class<?> clazz, String fieldAlias) { if ("MediaId".equals(fieldAlias)) { return true; // 特例添加 morning99 } // scan fields Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { // 1. exists XStreamCDATA if (field.getAnnotation(XStreamCDATA.class) != null) { XStreamAlias xStreamAlias = field.getAnnotation(XStreamAlias.class); // 2. exists XStreamAlias if (null != xStreamAlias) { if (fieldAlias.equals(xStreamAlias.value()))// matched return true; } else {// not exists XStreamAlias if (fieldAlias.equals(field.getName())) return true; } } } return false; } }
输出实体类 OutputMessage
package com.mor.maven.demo.mavenweb.model; import com.thoughtworks.xstream.annotations.XStreamAlias; /** * * @author morning * @date 2015年2月16日 下午2:29:32 */ @XStreamAlias("xml") public class OutputMessage { @XStreamAlias("ToUserName") @XStreamCDATA private String ToUserName; @XStreamAlias("FromUserName") @XStreamCDATA private String FromUserName; @XStreamAlias("CreateTime") private Long CreateTime; @XStreamAlias("MsgType") @XStreamCDATA private String MsgType = "text"; private ImageMessage Image; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public Long getCreateTime() { return CreateTime; } public void setCreateTime(Long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public ImageMessage getImage() { return Image; } public void setImage(ImageMessage image) { Image = image; } }
图片信息实体类
package com.mor.maven.demo.mavenweb.model; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Image") public class ImageMessage extends MediaIdMessage { }
多媒体id 实体类
package com.mor.maven.demo.mavenweb.model; import com.thoughtworks.xstream.annotations.XStreamAlias; public class MediaIdMessage { @XStreamAlias("MediaId") @XStreamCDATA private String MediaId; public String getMediaId() { return MediaId; } public void setMediaId(String mediaId) { MediaId = mediaId; } }
基本就这些类,也不知道拷贝全没有。
不过在输出xml的时候由于要添加CDATA标签所以没有实现完美,目前自己在SerializeXmlUtil 内添加了一下判断
如果是子标签下的值目前只能用这种方法加CDATA,不知道各位同学有没有好的方法。
目前只是实现了服务器认证,接收文本信息并回复原文本信息加上些附加信息,接收图片信息并返回原图片信息。
后期会有扩展,先记录到此。
以下内容,仅限于根据自己开发以及阅读微信文档总结,错误之处敬请指出,共同进步!
一、微信公众平台、微信公众平台.小程序、微信.开放平台登录地址
项目 | 微信公众平台 | 微信公众平台.小程序 | 微信。开放平台 |
---|---|---|---|
登录地址 | https://mp.weixin.qq.com | https://mp.weixin.qq.com | https://open.weixin.qq.com |
登录账号 | sensor@**** | 1960314645**** | 2851513591**** |
简称 | 公众号平台 | 小程序平台 | 开放平台 |
微信认证 | 每年年审,费用 300元/年 | 无认证费用 | 需要一次性认证,认证费300元 |
这三个平台必须使用不同的账号申请,因为账号是邮箱地址,所以,必须使用3个不同的邮箱地址作为账号,如果某个邮箱地址已经是微信公众平台的账号,则这个邮箱地址就不能用于另外的两个平台。
事实上,微信公众平台为了和微信公众平台.小程序区分开来,也可以称作微信公众平台.公众号。以下简称:公众号平台、小程序平台、开放平台。
如果需要搞清楚三者之间的关系,就涉及到unionid。关于unionid,在【微信官方文档.公众号】是这样描述的:开发者可通过OpenID来获取用户基本信息。特别需要注意的是,如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号,用户的unionid是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,unionid是相同的。
综上结合自己的理解:
1、公众号平台负责管理公众号;
2、小程序平台负责管理小程序;
3、每个平台对于某个用户都会产生一个OpenID,在单独的平台上这个OpenID是唯一的,但是如果某个用户既要使用公众号,又有小程序,那么在公众号和小程序平台上OpenID是分别不一样的。
4、此时就需要开放平台,凡是在开放平台登记的应用,对于这个开放平台都将使用一个统一的unionID。这样就可以在不同的平台上唯一确定某个用户了。
这么理解,公众号平台和小程序平台是相互独立的平台,可以各自开发各自的应用,如果是一个独立的应用,那么只需要使用OpenID就可以区别用户,例如如果只是使用公众号,不使用小程序,那么直接使用公众号的OpenID就可以了,繁殖对于小程序来说也是一样的。同一个用户在公众号平台和在小程序平台上的OpenID是不一样的,但是实际应用是经常会需要公众号和小程序同时使用,这样就需要使用开放平台来统一OpenID,最终出现了unionid。这个是唯一的,不变的!
按照官方文档的说明,前端是可以通过 wx.login 获取到 code 登录凭证,然后在后台通过 auth.code2Session 换取openid,session_key,unionid,需要说明的是,unionid是用户在开放平台的唯一标识符,在满足 UnionID 下发条件的情况下会返回。那么满足什么样的条件会直接返回unionid呢?答案是注册认证微信开放平台,注意一定要认证,也就是说要交300元通过认证,这样后台就会直接返回unionid。否则,按照官方说的如果没有返回unionid,也可以通过session_key在后台进行解密,解密需要的相关参数需要前端通过wx.getUserInfo调用获取到userInfo,rawData,signature,encryptedData,iv,cloudID参数。后台通过加密算法解密,最终可以得到unionid。 我测试的时候,如果没有通过开放平台认证,密文数据中始终没有返回unionid,不知道不认证的情况下满足什么样的条件才会返回。
平台简介
微信公众平台是腾讯公司在微信的基础上新增的功能模块,通过这一平台,个人和企业都可以打造一个微信的公众号,可以群发文字、图片、语音、视频、图文消息五个类别的内容。目前,微信公众平台支持PC,移动互联网网页登陆,并可以绑定私人账号进行群发信息。
微信公众平台是一个自媒体平台,他是微信系统的重要组成部分,微信整个板块包含个人微信、二维码、公众平台。
微信公众平台是腾讯公司布局电商网络的重要一步,2014年,腾讯公司将其升级为公司战略级。微信公众平台分为微信大陆版和微信海外版。
曾命名为“官号平台”和“媒体平台”,最终定位为“公众平台”。和新浪微博早起从明星战略着手不同,微信此时已经有了亿级的用户,挖掘自己用户的价值,为这个新的平台增加更优质的内容,创造更好的粘性,形成一个不一样的生态循环,是平台发展初期更重要的方向。
微信公众平台主要面向名人、政府、媒体、企业等机构推出的合作推广业务。在这里可以通过渠道将品牌推广添加上平台作用。微信公众平台于2012年08月23日正式上线,创造更好的用户体验,形成一个不一样的生态循环。
微信在2013年08月05日从4.5版本升级到了5.0版本,同时,微信公众平台也做了大幅调整,微信公众账号被分成订阅号和服务号,运行主题是组织(比如企业、媒体、公益组织)的,可以申请服务号,运营主题是组织和个人的可以申请订阅号,但是个人不能申请服务号。
不同账号的区别:
一、服务号的功能:公众平台服务号,是公众平台的一种账号类型,旨在为用户提供服务。
二、订阅号的功能
公众平台订阅号,是公众平台的一种账号类型,为用户提供信息和咨询。如:新闻等。
公众平台消息接口为开发者提供与用户进行消息交互的能力。对于成功借入消息接口的公众账号,当用户发消息给公众号,微信公众平台服务器会使用http请求对借入的网址记性消息推送,第三方服务器可通过相应包回复特定结构,从而达到回复消息的目的。
腾讯微信公众平台开放接口,是基于腾讯微信公众平台,为广大开发者和用户提供的开放数据分享与传播平台。广大开发者和用户登录平台后,就可以使用平台提供的开放API几口,使用姐扩实现诸如自动回复、消息推送等功能。
平台简介