springmvc 整合微信
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 整合微信的更多相关文章
- (转)Dubbo与Zookeeper、SpringMVC整合和使用
原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...
- Mate7微信指纹支付来了 比Touch ID整合微信早一点
之前我们聊过微信将推指纹支付 "指付通"会与Touch ID整合吗这个话题,现在有国内厂商率先支持微信指纹支付,体验一下美国用户使用Apple Pay搭配Touch ID来实现便捷 ...
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- SpringMVC整合Tiles框架
SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...
随机推荐
- cmake实战第二篇:让我们的代码更像个工程
为工程添加以下文件夹: bin 用来放编译好的可执行二进制文件. src 用来放源代码. lib 用来放编译好的库文件. include 用来放头文件. sudo mkdir -p /code_ ...
- android实现解析webservices
package com.example.ksoap2demo; import java.io.UnsupportedEncodingException; import org.ksoap2.SoapE ...
- 【BZOJ1951】[Sdoi2010]古代猪文 Lucas定理+CRT
[BZOJ1951][Sdoi2010]古代猪文 Description 求$X=\sum\limits_{d|n}C_n^d$,$Ans=G^X (\mod 999911659)$. Input 有 ...
- 修改sudoers权限之后无法sudo的最简单解决方法
网上百度一下进入recovery模式或是单用户模式仍然修改不了sudoers的权限, 后来终于在网上找到了一种最简单的方法,那就是 pkexec chmod 0440 /etc/sudoers
- cmake window下 sh.exe was found in your PATH, here
在window下 mingw环境下 用 camke 编译Cpp程序 CMake Error at D:/Program Files/CMake/share/cmake-3.8/Modules/CMak ...
- 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析
w http://www.jb51.net/article/57238.htm
- atob, slice,bin2hex,escape
JS处理二进制数据 http://phpor.net/blog/post/1898
- Spring Data 关于Repository的介绍(四)
Repository类的定义: public interface Repository<T, ID extends Serializable> { } 1)Repository是一个空接口 ...
- xshell上传下载文件
上传文件到服务器rz 从服务器下载文件sz
- 怎样优雅的管理ActionBar
转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/50997095 本文出自:[顾林海的博客] 前言 随着项目越来越大.页面 ...