springmvc 整合微信



方式一:
① 配置验证
@RequestMapping(value = "/into", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public void validate(WeChat wc, PrintWriter out) {
String signature = wc.getSignature(); // 微信加密签名
String timestamp = wc.getTimestamp(); // 时间戳
String nonce = wc.getNonce();// 随机数
String echostr = wc.getEchostr();// 随机字符串
System.out.println("加密的签名字符串:" + signature);
System.out.println("时间戳:" + timestamp);
System.out.println("随机数:" + nonce);
System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
// 随机字符串
System.out.println("我进来了");
out.print(echostr);
} else {
System.out.println("不是微信服务器发来的请求,请小心!");
}
out.flush();
out.close();
}
② 数据处理
@RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
public void dispose(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// 调用核心业务类接收消息、处理消息
// 从请求中读取整个post数据
// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
InputStream inputStream = req.getInputStream();
String datapacket = IOUtils.toString(inputStream, "UTF-8");
String respMessage = CoreService.processRequest(datapacket);
log.info(respMessage);
// 响应消息
out.print(respMessage);
out.close();
} 方式二:
@RequestMapping(value = "/into", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
@ResponseBody
public String validate(WeChat wc) {
String signature = wc.getSignature(); // 微信加密签名
String timestamp = wc.getTimestamp(); // 时间戳
String nonce = wc.getNonce();// 随机数
String echostr = wc.getEchostr();// 随机字符串
System.out.println("加密的签名字符串:" + signature);
System.out.println("时间戳:" + timestamp);
System.out.println("随机数:" + nonce);
System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
// 随机字符串
System.out.println("我进来了");
return echostr;
} else {
System.out.println("不是微信服务器发来的请求,请小心!");
return "error";
}
}
@RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
public void dispose(HttpServletRequest req, WeChat wc,
HttpServletResponse resp) throws IOException {
String signature = wc.getSignature(); // 微信加密签名
String timestamp = wc.getTimestamp(); // 时间戳
String nonce = wc.getNonce();// 随机数
String echostr = wc.getEchostr();// 随机字符串
System.out.println("加密的签名字符串:" + signature);
System.out.println("时间戳:" + timestamp);
System.out.println("随机数:" + nonce);
System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
// 调用核心业务类接收消息、处理消息
// 从请求中读取整个post数据
InputStream inputStream = req.getInputStream();
String datapacket = IOUtils.toString(inputStream, "UTF-8");
String respMessage = CoreService.processRequest(datapacket);
log.info(respMessage);
// 响应消息
out.print(respMessage);
}
out.flush();
out.close();
} 方式三:(推荐使用)
@RequestMapping(value = "/into", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
@ResponseBody
public String validate(WeChat wc) {
String signature = wc.getSignature(); // 微信加密签名
String timestamp = wc.getTimestamp(); // 时间戳
String nonce = wc.getNonce();// 随机数
String echostr = wc.getEchostr();// 随机字符串
if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
// 随机字符串
log.info("我进来了");
return echostr;
} else {
log.error("不是微信服务器发来的请求,请小心!");
return "error";
}
}
@RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
@ResponseBody
public String dispose(HttpServletRequest req, WeChat wc) throws IOException {
String signature = wc.getSignature(); // 微信加密签名
String timestamp = wc.getTimestamp(); // 时间戳
String nonce = wc.getNonce();// 随机数
if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
// 调用核心业务类接收消息、处理消息
// 从请求中读取整个post数据
InputStream inputStream = req.getInputStream();
String datapacket = IOUtils.toString(inputStream, "UTF-8");
String respMessage = CoreService.processRequest(datapacket);
log.info(respMessage);
// 响应消息
return respMessage;
}else{
log.error("数据处理失败!");
return "error";
}
}

springmvc 整合微信的更多相关文章

  1. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  2. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  3. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  4. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  6. Mate7微信指纹支付来了 比Touch ID整合微信早一点

    之前我们聊过微信将推指纹支付 "指付通"会与Touch ID整合吗这个话题,现在有国内厂商率先支持微信指纹支付,体验一下美国用户使用Apple Pay搭配Touch ID来实现便捷 ...

  7. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  8. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  9. SpringMVC整合Tiles框架

    SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...

随机推荐

  1. 鱼眼镜头的distortion校正【matlab】

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 作者:WWC %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  2. uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

    option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...

  3. C语言必掌握知识点

    个人总结,学c的赶快看 1-.++a 和 a++ 的差别:           ++a  先加在赋值  a++ 先赋值在加  后者赋给变量b的值为a而不是a+1后的值 2-.按位与  同为1时为1,其 ...

  4. iOS 保存异常日志

    // // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...

  5. SpringMVC如何接收json数据

    请求头:Content-Type=application/json数据如: {"mobile":"12345678912","smsContent&q ...

  6. SpringBoot专题1----springboot与mybatis的完美融合

    springboot大家都知道了,搭建一个spring框架只需要秒秒钟.下面给大家介绍一下springboot与mybatis的完美融合: 首先:创建一个名为springboot-mybatis的ma ...

  7. Byzantine failures

    https://baike.baidu.com/item/拜占庭将军问题/265656?fr=aladdin 拜占庭将军问题(Byzantine failures),是由莱斯利·兰伯特提出的点对点通信 ...

  8. tortoisegit错误: disconnected - no supported authentication methods available(server sent: publickey)

    修改小乌龟的 SSH客户端:

  9. shell脚本杂

    1.sh -x 跟踪shell脚本中的每个命令 [root@master shellexer]# cat bash.sh #!/bin/bash var=$ echo $var [root@maste ...

  10. 浅谈REDIS数据库的键值设计(转)

    add by zhj: 关系数据库表的一条记录可以映射成Redis中的一个hash类型,其实数据库记录本来就是键值对.这样,要比本文中的键设计用更少的键,更节省内存,因为每个键除了它的键值占用内存外, ...