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页面提供了一种模板机制,它能将网页的布局和内容分离 ...
随机推荐
- yalmip + lpsolve + matlab 求解混合整数线性规划问题(MIP/MILP)
最近建立了一个网络流模型,是一个混合整数线性规划问题(模型中既有连续变量,又有整型变量).当要求解此模型的时候,发现matlab优化工具箱竟没有自带的可以求解这类问题的算法(只有bintprog求解器 ...
- 【Raspberry pi】python ide-spyder
sudo apt-get install spyder 简单 明了
- GL 纹理 格式 资料备份
分别转载至:http://www.tuicool.com/articles/qAbYfq 和 http://www.verydemo.com/demo_c161_i114362.html 在手机 ...
- BZOJ 3362 Navigation Nightmare 带权并查集
题目大意:给定一些点之间的位置关系,求两个点之间的曼哈顿距离 此题土豪题.只是POJ也有一道相同的题,能够刷一下 别被题目坑到了,这题不强制在线.把询问离线处理就可以 然后就是带权并查集的问题了.. ...
- Django项目实战 - 搜索功能(转)
首先,前端已实现搜索功能页面, 我们直接写后台逻辑: Q()可以实现 逻辑或的判断, name_ _ icontains 表示 name字段包含搜索的内容,i表示忽略大小写. from djang ...
- 最近5年133个Java面试问题列表
Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来越高级,面试官问的问题也更深入. 在我 ...
- log4j中将SocketAppender将日志内容发送到远程服务器
1.服务端配置 1)服务端配置文件log4j-server.properties #Define a narrow log category. A category like debug will p ...
- 【谷歌浏览器】在任意页面运行JS
1.使用谷歌浏览器的调试功能: 在任何页面上运行代码片段 · Chrome 开发者工具中文文档 注:比较简单,直接,不过只能本地执行,只能自己使用.且需自行保存JS文件: 2.使用油猴插件: Tamp ...
- 【Python之路】第二十篇--MySQL(二)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1.创建视图 --格式:CREATE ...
- Maven 整合SSH框架之pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...