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. WPF通过异常来验证用户输入

    在WPF中使用数据绑定,如果用户输入和绑定类型转换失败,控件就会显示出现错误的模板, 比如一个Textbox绑定到一个int 属性,如果用户输入一个string,那这个textbox就会显示错误模板, ...

  2. (一)Spring容器相关操作

    一.spring事件 spring的事件有如下两个成员. 1.ApplicationEvent,容器事件,由容器发布 2.ApplicationListener 监听器,可以由容器中的任何监听器Bea ...

  3. call to unavailable function system not available on ios 解决方案

    编译时报错:call to unavailable function system not available on iOS 原因:iOS11已经将system删除 解决方案:system(comma ...

  4. 【VUE】VUE相关学习和知识备份

    一.学习资料参考 1.1.Vue.js 官网:Vue.js https://cn.vuejs.org/ 官方文档:介绍 - Vue.js https://cn.vuejs.org/v2/guide/ ...

  5. Create a Group Policy Central Store

    一.How to create a Group Policy Central Store You have downloaded or created your own Group Policy Ad ...

  6. CSRF Laravel

    Laravel 使得防止应用 遭到跨站请求伪造攻击变得简单. Laravel 自动为每一个被应用管理的有效用户会话生成一个 CSRF “令牌”,该令牌用于验证授权用 户和发起请求者是否是同一个人. 任 ...

  7. sqlalchemy(二)高级用法 2

    转自:https://www.cnblogs.com/coder2012/p/4746941.html 外键以及relationship 首先创建数据库,在这里一个user对应多个address,因此 ...

  8. FatMouse and Cheese---hdu1078(记忆化搜索=搜索+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意就是有n*n的地图,每个地方都有食物,数量不同,老鼠在(0,0)的位置每次它最多跳 k 步, ...

  9. .net 存储过程中的 output参数取值问题

    当存储过程中多个结果需要返回时经常需要用到output类型的参数,如果存储过程没有返回结果集只是输出output类型参数时使用如下代码: db.AddOutParameter(dbCmd, " ...

  10. Redis几个认识误区(转)

    add by zhj: 文章很老了,2010年的,注意,下面几点是作者认为的误区 原文:http://timyang.net/data/redis-misunderstanding/ 前几天微博发生了 ...