一、背景

最近我的项目中因为某些起因,波及到了微信公众号的开发,此处简略记录下微信公众号的接入。

二、前置条件

1、须要有一个能够在外网能够拜访的域名
2、本地开发,须要将内网环境穿透到外网能够拜访。
3、须要有一个公众号。

留神:
1、内网穿透外网域名 咱们能够通过 natapp 来购买实现。

三、通过natapp内网穿透和域名购买

1、购买隧道


此处依据本人的状况,购买一个适宜本人的隧道。

前期将会把咱们本人的本地端口,映射到一个外网能够拜访的网址上。

2、购买一个二级域名(如果本人有能够疏忽)


留神
1、此处购买一个 二级域名,如果本人有 域名,能够不必购买。

3、将域名绑定到购买的隧道上

4、下载natapp客户端

https://natapp.cn/#download
此处须要依据本人的操作系统,下载对应的客户端。

5、启动 natapp


此处的 authtoken 的值为 咱们本人购买的隧道的值。

四、编写微信服务端接入验证

1、填写服务器配置

门路:开发->根本配置

2、编写服务端验证接入代码


微信服务器地址URL:这个能够先记下来,上面会告知在那个中央配置
咱们本人接入微信公众号有些验证比拟麻烦,此处借助网上的开源框架 weixin-java-mp 这个程序开发

1、引入 weixin-java-mp.jar

<dependency>    <groupId>com.github.binarywang</groupId>    <artifactId>weixin-java-mp</artifactId>    <version>4.0.0</version></dependency>

2、进行 mp 配置

@Configurationpublic class WxMpConfiguration {    @Autowired    private WxMpProperties wxMpProperties;    @Bean    public WxMpService wxMpService() {        WxMpServiceImpl wxMpService = new WxMpServiceImpl();        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());        // 设置多个微信公众号的配置        // wxMpService.setMultiConfigStorages();        return wxMpService;    }    /**     * 这个中央的配置是保留在本地,生产环境须要本人扩大,能够保留在Redis中等等     *     * @return WxMpConfigStorage     */    public WxMpConfigStorage wxMpConfigStorage() {        WxMpDefaultConfigImpl storage = new WxMpDefaultConfigImpl();        storage.setAppId(wxMpProperties.getAppId());        storage.setSecret(wxMpProperties.getAppSecret());        storage.setAesKey(wxMpProperties.getAesKey());        storage.setToken(wxMpProperties.getToken());        return storage;    }}

留神
1、WxMpConfigStorage 这个类在生产环境中,如果服务是 集群 部署的话,最好不要应用 WxMpDefaultConfigImpl 因为这个是将配置保留在 内存中。能够思考应用 WxMpRedissonConfigImpl 这个类。
2、WxMpConfigStorage 中的配置,和上方的服务器配置保持一致。

3、服务器端代码验证

@Component@RestController@Slf4jpublic class MpEntryController {    @Autowired    private WxMpService wxMpService;    /**     * 微信接入     *     * @param signature 签名     * @param timestamp 工夫戳     * @param nonce     随机数     * @param echoStr   随机字符串     * @return 接入胜利返回 echoStr 的值,否则轻易返回     */    @GetMapping("/mp/entry")    public String entry(@RequestParam("signature") String signature,                        @RequestParam("timestamp") String timestamp,                        @RequestParam("nonce") String nonce,                        @RequestParam("echostr") String echoStr) {        log.info("微信公众号/服务号接入传递的参数 signature:[{}],timestamp:[{}],nonce:[{}],echostr:[{}]",                signature, timestamp, nonce, echoStr);        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echoStr)) {            log.error("接管到微信认证信息,参数非法,存在为空的参数");            return "error";        }        boolean result = wxMpService.checkSignature(timestamp, nonce, signature);        log.info("微信公众号/服务号接入胜利?[{}]", result);        return result ? echoStr : "error";    }}

留神
1、/mp/entry 这个为咱们本人的接入门路,和上方图中保留统一。

4、验证

1、启动咱们的web工程
2、在微信配置页面,点击提交
3、验证通过,阐明接入胜利。


微信服务器地址URL 就是 /mp/entry

五、参考文档

1、微信接入

六、代码网址

https://gitee.com/huan1993/wechat-development