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. CentOS7 以下安装Mysql MMM

    參考文档 參考1 參考2 * 一. 规划 server IP 作用 monitor 192.168.1.210 监控server master-001 192.168.1.211 读写主机001 ma ...

  2. DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016错误解决方法

    这个错误是:表处于"装入暂挂"状态. 经多次尝试 总结方法: 1:reorg table <表>: 假如不好使 则下面方法 2,先前尝试装入(LOAD)此表失败.表的状 ...

  3. Multiview

    新建3个ViewController的类 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button ...

  4. apache的ab命令做压力测试

    1. 最基本的关心两个选项 -c -n 例: ./ab -c 100 -n 10000 http://127.0.0.1/index.php -c 100 即:每次并发100个-n 10000 即: ...

  5. ural1297. Palindrome

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  6. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  7. hibernate中inverse作用

    默认 inverse="false"即该元素指向的类负责维护该关系. 如: <hibernate-mapping> <class name="com.h ...

  8. Ubuntu 16.04 安装google浏览器

    因为安装的Linux是64位的Ubuntu 16.04系统,所以本人决定也安装64位的谷歌Chrome浏览器.在 Ubuntu 16.04 中,要想使用谷歌的 Chrome 浏览器,可以通过命令行的方 ...

  9. Making training mini-batches

    Here is where we'll make our mini-batches for training. Remember that we want our batches to be mult ...

  10. python的socket的学习

    一.Socket相关知识 1.socket是什么: socket是应用层与TCP/IP协议族通信的中间软件抽象层,他是一组接口.在设计模式中,Socket其实就是一个门面模式. 它把复杂的TCP/IP ...