1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>demo</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2.新建一个RestController

import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* 微信接入
*/
@RestController
public class WeiXinController { // private final String appid='';
// private final String secret=''; private final String appid = "";
private final String secret = ""; private final String token = "demo"; private static Logger logger = LoggerFactory.getLogger(WeiXinController.class); @GetMapping("/wx")
public String getWeiXin(@RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) { logger.debug("signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr);
if (StringUtils.isEmpty(signature)) {
return "";
} String[] arrs = new String[] { token, timestamp, nonce };
Arrays.sort(arrs); String tmp = String.join("", arrs);
//使用common.codec进行sah1加密
String hashCode = DigestUtils.sha1Hex(tmp); if (StringUtils.equals(signature, hashCode)) {
return echostr;
} return "";
} @PostMapping(value = "/wx")
public String postWeiXin(@RequestBody String xmlMsg) { logger.debug("xmlMsg:{}", xmlMsg); //获取消息类型
String pattern = "<MsgType(?:><!\\[CDATA\\[|>)(.*?)(?:\\]\\]><|<)/MsgType>";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(xmlMsg);
String msgType = ""; if (m.find()) {
msgType = m.group(1);
} if ("text".equals(msgType)) {
//使用xstream进行转换
XStream xStream = new XStream();
//XStream.setupDefaultSecurity(xStream);
//xStream.allowTypes(new Class[]{TextMsg.class});
xStream.processAnnotations(TextMsg.class);
TextMsg txtMsg = (TextMsg) xStream.fromXML(xmlMsg); // 示例 交换发送接收人,然后把源信息返回
TextMsg replyMsg = (TextMsg) xStream.fromXML(xmlMsg);
replyMsg.setToUserName(txtMsg.getFromUserName());
replyMsg.setFromUserName(txtMsg.getToUserName()); //返回
return xStream.toXML(replyMsg);
} else {
return "other msg ";
}
} @XStreamAlias("xml")
public static class TextMsg implements Serializable { private static final long serialVersionUID = 1L;
private String MsgType;
private String ToUserName;
private String FromUserName;
private Long CreateTime;
private String MsgId;
private String Content; /**
* @return the msgType
*/
public String getMsgType() {
return MsgType;
} /**
* @return the content
*/
public String getContent() {
return Content;
} /**
* @param content the content to set
*/
public void setContent(String content) {
this.Content = content;
} /**
* @return the msgId
*/
public String getMsgId() {
return MsgId;
} /**
* @param msgId the msgId to set
*/
public void setMsgId(String msgId) {
this.MsgId = msgId;
} /**
* @return the createTime
*/
public Long getCreateTime() {
return CreateTime;
} /**
* @param createTime the createTime to set
*/
public void setCreateTime(Long createTime) {
this.CreateTime = createTime;
} /**
* @return the fromUserName
*/
public String getFromUserName() {
return FromUserName;
} /**
* @param fromUserName the fromUserName to set
*/
public void setFromUserName(String fromUserName) {
this.FromUserName = fromUserName;
} /**
* @return the toUserName
*/
public String getToUserName() {
return ToUserName;
} /**
* @param toUserName the toUserName to set
*/
public void setToUserName(String toUserName) {
this.ToUserName = toUserName;
} /**
* @param msgType the msgType to set
*/
public void setMsgType(String msgType) {
this.MsgType = msgType;
}
} }

微信公众号配置   http://ip或域名/wx

第三方平台开发 全网发布时验证注意事项

参考 https://blog.csdn.net/dk947960731/article/details/52204408

java 接入微信 spring boot 接入微信的更多相关文章

  1. Spring Boot 开发微信公众号后台

    Hello 各位小伙伴,松哥今天要和大家聊一个有意思的话题,就是使用 Spring Boot 开发微信公众号后台. 很多小伙伴可能注意到松哥的个人网站(http://www.javaboy.org)前 ...

  2. 第64节:Java中的Spring Boot 2.0简介笔记

    Java中的Spring Boot 2.0简介笔记 spring boot简介 依赖java8的运行环境 多模块项目 打包和运行 spring boot是由spring framework构建的,sp ...

  3. 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案

    技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...

  4. 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案--temp

    技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...

  5. Spring Boot中微信全局token的缓存实现

    为什么要缓存token? 这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN.网页授权Token每天的调用次数没有限制,不需要缓存. 接口 ...

  6. Spring Boot接入 apollo 后启动 dubbo 报错

    原文地址:https://xobo.org/spring-boot-apollo-dubbo-xml-error/ 某Spring Boot项目接入 apollo 后启动 dubbo 报错Caused ...

  7. Spring Boot项目微信云托管入门部署

    微信云托管本身是一个服务器,里面的软件都已经配置好了,直接使用即可,适用于一些简单部署的项目.直接把项目直接上传到服务器即可.无需各种繁琐的软件配置和打包,微信云托管统统给你搞定.而且系统会根据使用量 ...

  8. Spring Boot企业微信点餐系统

    第1章 课程介绍 包括项演示.课程概述.课程安排.学习前提等的介绍, 让同学们了解这课程 1-1 课程介绍 第2章 项目设计 包括需求分析,项?目设计,项?目架构,数据库设计等等. 2-1 项目设计 ...

  9. Spring Boot企业微信点餐系统-第一章-课程介绍

    一.项目简介——技术要点 前端和后端: 后端主要技术: 微信接口技术 微信支付 微信扫码登录 微信模板消息推送 开发环境 但实际上我用的环境和这上面还是有点不一样,我服务器用的是win,到时候我会详细 ...

随机推荐

  1. java面向对象1-面向对象概念

    面向对象概念-类与对象的关系 封装:指隐藏对象的属性和实现细节,仅对外提供公共访问方式,private-构造方法/构造器-this关键字-static关键字(javadoc制作工具类) -代码块 继承 ...

  2. Docker(2)--Centos7 上安装部署

    Centos7 上安装docker Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比 ...

  3. 虚拟机使用桥接模式连接网络并且设置静态ip

    1.桥接模式连接网络 虚拟机连接网络一共有四种模式,我这里只介绍桥接模式,毕竟坑了我几个小时 设置有线连接,我本来用的无线连接完成微信点餐系统,后来换了有线因为有线连接不会分配ip,和本地电脑使用同一 ...

  4. JPA学习(三、JPA_API)

    框架学习之JPA(三) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...

  5. 监听ios自带返回功能

    //1.一开始用这段代码,结果发现滚动都失效了,如果页面大于屏幕高度将无法滚动至底部,所以淘汰 // document.addEventListener('touchmove', function(e ...

  6. css了解一下!!!

    css简介 css(cascading style sheet,层叠样式表):为了让网页的内容核样式拆分开; 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染); css语法 cs ...

  7. Android 通过应用设置系统日期和时间的方法

    Android 通过应用设置系统日期和时间的方法 android 2.3 android 4.0 测试可行,不过需要ROOT权限. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. [IOI2008/BZOJ1791 岛屿](处理基环树的小技巧&基于bfs树形DP)

    IOI2008/BZOJ1791 岛屿 题目大意是在一个基环树森林里求每一棵基环树的直径①的和. 其实就是树的直径的基环树升级版.我们先把环找出来,然后从环上的每一个节点x出发,并且不经过环上其他节点 ...

  9. 关于判断StringBuffer是否为空

    对于String和StringBuffer来说,都是通过创建新的char value[]数组来达到字符串改变的操作的,只不过String是通过新创建String对象来达到目的, 而StringBuff ...

  10. win10笔记本设置管理员权限

    1.在右下方任务栏的“搜索web和windows”输入框中输入“gpedit.msc”,电脑会自行搜索,搜索完毕之后鼠标点击打开.