微信小程序消息推送服务器接入
2018-03-05 14:56
447 查看
1、消息转发地址填写,需要注意的是token和encodingaeskey要和项目中配置一致
2、URL要实现两个路由相同的方法但是一个需要时GET另一个是POST,如下 @ResponseBody
@RequestMapping(method=RequestMethod.GET,produces= "text/plain;charset=utf-8")
public void authGet(@RequestParam(name = "signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr,HttpServletResponse response) throws Exception {
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
String[] str = { TOKEN, timestamp, nonce };
Arrays.sort(str); // 字典序排序
String bigStr = str[0] + str[1] + str[2];
// SHA1加密
SHA1 sha1= new SHA1();
String digest =sha1.gen(str);
// 确认请求来至微信
if (digest.equals(signature)) {
response.getWriter().print(echostr);
}
}
2、URL要实现两个路由相同的方法但是一个需要时GET另一个是POST,如下 @ResponseBody
@RequestMapping(method=RequestMethod.GET,produces= "text/plain;charset=utf-8")
public void authGet(@RequestParam(name = "signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr,HttpServletResponse response) throws Exception {
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
String[] str = { TOKEN, timestamp, nonce };
Arrays.sort(str); // 字典序排序
String bigStr = str[0] + str[1] + str[2];
// SHA1加密
SHA1 sha1= new SHA1();
String digest =sha1.gen(str);
// 确认请求来至微信
if (digest.equals(signature)) {
response.getWriter().print(echostr);
}
}
/** * POST请求: * 接收客服消息; * 此处的value值也就是往微信公众平台要配置的请求的地址 */ @ResponseBody @RequestMapping(method=RequestMethod.POST) public JSONObject doMessagePost(HttpServletRequest request,HttpServletResponse response) { try { ServletInputStream stream = request.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuffer buffer = new StringBuffer(); String line = new String(""); while((line = reader.readLine()) != null){ buffer.append(line); } JSONObject jsonObject = JSONObject.fromObject(buffer.toString()); String to=jsonObject.getString("ToUserName"); String from=jsonObject.getString("FromUserName"); String time=jsonObject.getString("CreateTime"); String sessionFrom=jsonObject.getString("SessionFrom"); String msgType=jsonObject.getString("MsgType"); PrintWriter out=response.getWriter(); String msg="<xml>\r\n" + " <ToUserName><![CDATA["+to+"]]></ToUserName>\r\n" + " <FromUserName><![CDATA["+from+"]]></FromUserName>\r\n" + " <CreateTime>"+time+"</CreateTime>\r\n" + " <MsgType><![CDATA[transfer_customer_service]]></MsgType>\r\n" + " </xml>"; out.print(msg); if("event".equals(msgType)&&"follow".equals(sessionFrom)) { pushService.setSendCustomerMsgLink(from); } } catch (Exception e) { e.printStackTrace(); } return null; }第一个方法用于验证,第二个方法用于接收消息转发和其他业务逻辑实现,方法二中也实现了接收到的客服消息转发给客服,关键点在于
<MsgType><![CDATA[transfer_customer_service]]></MsgType>至此也就全部完成了。
相关文章推荐
- 微信小程序 消息推送php服务器验证实例详解
- 微信小程序之消息推送配置Token验证失败-已解决
- 啊Ran讲微信开发(.net) :订阅号+自定义服务器(URL接入,文本消息处理)
- 飞鸽快信微信消息-LINUX服务器告警、状态监控预警、程序报警提醒等消息实时推送工具
- 飞鸽快信微信消息-LINUX服务器告警、状态监控预警、程序报警提醒等消息实时推送工具
- 微信小程序服务端开发之消息推送
- 突破微信小程序模板消息的推送限制
- 突破微信小程序模板消息限制,实现无限制主动推送
- 微信小程序--消息推送配置Token令牌错误校验失败如何解决
- 微信小程序消息推送配置Token校验失败问题
- 微信推送给服务器的XML消息解析-springmvc 解析xml数据流
- 微信小程序开发:设置消息推送
- 用swoole搭建推送消息到小程序的服务器
- 微信小程序--消息推送配置Token令牌错误校验失败如何解决
- 微信服务号推送模板信息服务器验证与模板消息推送
- 突破微信小程序模板消息限制,实现无限制主动推送
- 通过异步处理程序构建的长连接消息推送机制,单台服务器可以轻松支持上万个并发连接
- 小程序模板消息发送(微信推送)
- 微信小程序之发送模板消息(通过openid推送消息给用户)
- [微信小程序]消息推送配置Token令牌错误校验失败