微信公众号(服务号)配置服务器

前言

在微信公众号(服务号)开发的准备阶段需要我们配置自己的服务器。

image-20201217131942035

文档地址:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

下载运行官方示例

https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Message_Encryption/Message_encryption_and_decryption.html

image-20201202171753034

下载示例代码后解压如下:

image-20201202172231504

把 src 的代码复制到项目并解决完包路径问题后运行 demo 的 main 方法。提示如下

1
2
3
4
5
6
7
8
9
D:\Java\jdk1.8.0_144\bin\java.exe "-javaagent:D:\Program Files\ideaIU-2019.2win\lib\idea_rt.jar=1346:D:\Program Files\ideaIU-2019.2win\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IdeaProjects\qingyun\plantform\target\classes;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.4.0\spring-boot-starter-data-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-jdbc\2.1.1\spring-data-jdbc-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-relational\2.1.1\spring-data-relational-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-commons\2.4.1\spring-data-commons-2.4.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-tx\5.3.1\spring-tx-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-context\5.3.1\spring-context-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-beans\5.3.1\spring-beans-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-jdbc\2.4.0\spring-boot-starter-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter\2.4.0\spring-boot-starter-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-logging\2.4.0\spring-boot-starter-logging-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;D:\Program Files\apache-maven-3.5.3\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jdbc\5.3.1\spring-jdbc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-web\2.4.0\spring-boot-starter-web-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-json\2.4.0\spring-boot-starter-json-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.0\spring-boot-starter-tomcat-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.39\tomcat-embed-core-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.39\tomcat-embed-websocket-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-web\5.3.1\spring-web-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-webmvc\5.3.1\spring-webmvc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-aop\5.3.1\spring-aop-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-expression\5.3.1\spring-expression-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.4\mybatis-spring-boot-starter-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.4\mybatis-spring-boot-autoconfigure-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis\3.5.6\mybatis-3.5.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\thymeleaf\extras\thymeleaf-extras-springsecurity5\3.0.4.RELEASE\thymeleaf-extras-springsecurity5-3.0.4.RELEASE.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-devtools\2.4.0\spring-boot-devtools-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot\2.4.0\spring-boot-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.0\spring-boot-autoconfigure-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar;D:\Program Files\apache-maven-3.5.3\repository\org\projectlombok\lombok\1.18.16\lombok-1.18.16.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-core\5.3.1\spring-core-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jcl\5.3.1\spring-jcl-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\commons-codec\commons-codec\1.13\commons-codec-1.13.jar" com.wzy.plantform.weixin.demo.Program
Exception in thread "main" java.lang.IllegalArgumentException: Last encoded character (before the paddings if any) is a valid base 64 alphabet but not a possible value
at org.apache.commons.codec.binary.Base64.validateCharacter(Base64.java:798)
at org.apache.commons.codec.binary.Base64.decode(Base64.java:477)
at org.apache.commons.codec.binary.BaseNCodec.decode(BaseNCodec.java:411)
at org.apache.commons.codec.binary.BaseNCodec.decode(BaseNCodec.java:395)
at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:694)
at com.wzy.plantform.weixin.aes.WXBizMsgCrypt.<init>(WXBizMsgCrypt.java:62)
at com.wzy.plantform.weixin.demo.Program.main(Program.java:32)
我加的依赖包:
1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version><!-- 后面改到 1.9 版本 -->
</dependency>

百度了下 说是包的版本问题。

于是参照 readme.txt 更改了 commons-codec 包的版本为 1.9 再次运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D:\Java\jdk1.8.0_144\bin\java.exe "-javaagent:D:\Program Files\ideaIU-2019.2win\lib\idea_rt.jar=1447:D:\Program Files\ideaIU-2019.2win\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IdeaProjects\qingyun\plantform\target\classes;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.4.0\spring-boot-starter-data-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-jdbc\2.1.1\spring-data-jdbc-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-relational\2.1.1\spring-data-relational-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-commons\2.4.1\spring-data-commons-2.4.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-tx\5.3.1\spring-tx-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-context\5.3.1\spring-context-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-beans\5.3.1\spring-beans-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-jdbc\2.4.0\spring-boot-starter-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter\2.4.0\spring-boot-starter-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-logging\2.4.0\spring-boot-starter-logging-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;D:\Program Files\apache-maven-3.5.3\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jdbc\5.3.1\spring-jdbc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-web\2.4.0\spring-boot-starter-web-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-json\2.4.0\spring-boot-starter-json-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.0\spring-boot-starter-tomcat-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.39\tomcat-embed-core-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.39\tomcat-embed-websocket-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-web\5.3.1\spring-web-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-webmvc\5.3.1\spring-webmvc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-aop\5.3.1\spring-aop-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-expression\5.3.1\spring-expression-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.4\mybatis-spring-boot-starter-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.4\mybatis-spring-boot-autoconfigure-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis\3.5.6\mybatis-3.5.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\thymeleaf\extras\thymeleaf-extras-springsecurity5\3.0.4.RELEASE\thymeleaf-extras-springsecurity5-3.0.4.RELEASE.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-devtools\2.4.0\spring-boot-devtools-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot\2.4.0\spring-boot-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.0\spring-boot-autoconfigure-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar;D:\Program Files\apache-maven-3.5.3\repository\org\projectlombok\lombok\1.18.16\lombok-1.18.16.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-core\5.3.1\spring-core-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jcl\5.3.1\spring-jcl-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar" com.wzy.plantform.weixin.demo.Program
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
at javax.crypto.Cipher.implInit(Cipher.java:805)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1396)
at javax.crypto.Cipher.init(Cipher.java:1327)
at com.wzy.plantform.weixin.aes.WXBizMsgCrypt.encrypt(WXBizMsgCrypt.java:129)
at com.wzy.plantform.weixin.aes.WXBizMsgCrypt.encryptMsg(WXBizMsgCrypt.java:213)
at com.wzy.plantform.weixin.demo.Program.main(Program.java:33)
Exception in thread "main" com.wzy.plantform.weixin.aes.AesException: aes加密失败
at com.wzy.plantform.weixin.aes.WXBizMsgCrypt.encrypt(WXBizMsgCrypt.java:140)
at com.wzy.plantform.weixin.aes.WXBizMsgCrypt.encryptMsg(WXBizMsgCrypt.java:213)
at com.wzy.plantform.weixin.demo.Program.main(Program.java:33)

Process finished with exit code 1

readme 里说了 要去下个什么文件放在 jre 下面。由于我 java 用的是 jdk8 所以我把下面的 jce-7 改成 jce-8 后下载文件。

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

参照 readme 把下下来的 jar 包放在 jdk/jre/lib/security 下 替换后 再次运行如下:ok了

1
2
3
4
5
6
7
8
9
10
D:\Java\jdk1.8.0_144\bin\java.exe "-javaagent:D:\Program Files\ideaIU-2019.2win\lib\idea_rt.jar=1655:D:\Program Files\ideaIU-2019.2win\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IdeaProjects\qingyun\plantform\target\classes;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.4.0\spring-boot-starter-data-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-jdbc\2.1.1\spring-data-jdbc-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-relational\2.1.1\spring-data-relational-2.1.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\data\spring-data-commons\2.4.1\spring-data-commons-2.4.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-tx\5.3.1\spring-tx-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-context\5.3.1\spring-context-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-beans\5.3.1\spring-beans-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-jdbc\2.4.0\spring-boot-starter-jdbc-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter\2.4.0\spring-boot-starter-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-logging\2.4.0\spring-boot-starter-logging-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;D:\Program Files\apache-maven-3.5.3\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jdbc\5.3.1\spring-jdbc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-web\2.4.0\spring-boot-starter-web-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-json\2.4.0\spring-boot-starter-json-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.0\spring-boot-starter-tomcat-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.39\tomcat-embed-core-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\Program Files\apache-maven-3.5.3\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.39\tomcat-embed-websocket-9.0.39.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-web\5.3.1\spring-web-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-webmvc\5.3.1\spring-webmvc-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-aop\5.3.1\spring-aop-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-expression\5.3.1\spring-expression-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.4\mybatis-spring-boot-starter-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.4\mybatis-spring-boot-autoconfigure-2.1.4.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis\3.5.6\mybatis-3.5.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\Program Files\apache-maven-3.5.3\repository\org\thymeleaf\extras\thymeleaf-extras-springsecurity5\3.0.4.RELEASE\thymeleaf-extras-springsecurity5-3.0.4.RELEASE.jar;D:\Program Files\apache-maven-3.5.3\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-devtools\2.4.0\spring-boot-devtools-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot\2.4.0\spring-boot-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.0\spring-boot-autoconfigure-2.4.0.jar;D:\Program Files\apache-maven-3.5.3\repository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar;D:\Program Files\apache-maven-3.5.3\repository\org\projectlombok\lombok\1.18.16\lombok-1.18.16.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-core\5.3.1\spring-core-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\org\springframework\spring-jcl\5.3.1\spring-jcl-5.3.1.jar;D:\Program Files\apache-maven-3.5.3\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar" com.wzy.plantform.weixin.demo.Program
加密后: <xml>
<Encrypt><![CDATA[VmeFwwY8J9xFYMVvL5gipXrxJAusR3v5CUOU6ZXaPP/kEOROdaBfgkpr55ZY4lKRXL7wFtvY1iACIMdkhWuJlkzVTVxxt9fvZkKeVoINaGpGLpfDMueu/kqrfB0Pab/n5V8Rc9te2OT3gy1VWU0kzQxMhsNKRljHKczYb5t7nQDa1n8E8md+K0xdLk9WRAnTJeOoeTUhZRDXuzgxAADHm2HS/DFHd+eh9QygTpWDmsUgLuaAwWRWlGuHgQwIN1wUkhEbQskHw57P7wa/uUWOlG/yDyrRLCy58iwshvKk2fTc0EBwkDLmmh9mOxROvJFoi61DTFGPqHcRUMoeNyfIrtQdbY5ffIZhZ8Cu1GNYz4MCNyZ216SH4M6mUdsEFMN9wk4rkbLZQxtbjkSdbZ00GURu939SgNItzidPeZeU6Nk3drYB4rW1+Eg5V0TjJu0Vee1OD0yRHbCaFMlvB7f5cGj2FKoY+Kggn8DkdmVx4TTmUKtZ6XLKS68v99BygNTmFBm56zVlwQLW7/ClqqWtnB3WW9Xt5eKi70j81kc5KcNZWiIHnTD2NDBSy952Y/dVR+XXNCfEXNbqFgF9smE0BONy/GD72VGARrdRASOkcps96qJ/1jJ5sMvph7Nb3S/e]]></Encrypt>
<MsgSignature><![CDATA[cdd207617899101c4bc79b612952f4b29b3f42e8]]></MsgSignature>
<TimeStamp>1409304348</TimeStamp>
<Nonce><![CDATA[xxxxxx]]></Nonce>
</xml>
解密后明文: 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>

Process finished with exit code 0

发布服务

把代码整理下发布一个 http 的服务。并把 url 地址填写到 微信的配置(url)里,提交即可。

1
2
3
4
5
6
7
@GetMapping("/verify-Url")
public String verifyUrl(String signature, String timestamp, String nonce, String echostr)
throws AesException {
log.info("msgSignature:{},timeStamp:{},nonce:{},echoStr:{}",signature,timestamp,nonce,echostr);
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
return pc.verifyUrl(signature, timestamp, nonce, echostr);
}

其中 pc.verifyUrl 方法就是 示例代码中 WXBizMsgCrypt 类的 verifyUrl 方法。

踩坑

配置完后一直验证失败!通过打印输出 发现签名一直不对!后百度到 echoStr 参数填写 “” (空串)。最终结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public String verifyUrl(String msgSignature, String timeStamp, String nonce, String echoStr)
throws AesException {
// String signature = SHA1.getSHA1(token, timeStamp, nonce, echoStr);
// SHA1.getSHA1 方法 echoStr 参数传 空串
String signature = SHA1.getSHA1(token, timeStamp, nonce, "");
System.out.println("微信传入的msgSignature:"+msgSignature);
System.out.println("代码生成的 Signature:"+signature);
if (!signature.equals(msgSignature)) {
throw new AesException(AesException.ValidateSignatureError);
}

// String result = decrypt(echoStr);
// return result;
// 只要 签名相同 直接返回原样 echoStr (微信开发文档有说明)
return echoStr;
}

文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

image-20201202222627199