java 接入微信 spring boot 接入微信

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

原文地址:https://www.cnblogs.com/liuxm2017/p/10174707.html