The background,
Due to some reasons, the recent project involves the development of WeChat official account. The access to WeChat official account is simply recorded here.
2. Preconditions
1, need to have a domain name can be accessed on the external network 2, local development, need to penetrate the internal network environment can be accessed on the external network. 3, need to have a public number.
Note: 1. Intranet penetration and extranet domain name can be purchased through NatApp.
III. Through NatApp Intranet penetration and domain name purchase
1. Buy tunnels
Here according to their own situation, buy a suitable for their own tunnel.
Later we will map our own local port to a URL that can be accessed by the external network.
2. Purchase a secondary domain name (if you have one, you can ignore it)
Pay attention to:
1, here to buy a secondary domain name, if you have a domain name, you can not buy.
3. Bind the domain name to the purchased tunnel
4. Download the NatApp client
https://natapp.cn/#download Here you need to download the corresponding client according to your own operating system.
5. Launch NatApp
Here theauthtoken
The value of the tunnel is the value of the tunnel we purchased ourselves.
Four, write WeChat server access verification
1. Fill in the server configuration
Path:Development -> basic configuration
2. Wrote the server verification access code
WeChat server address URL
: This can be written down, the following will tell you where to configureOur own access to the WeChat public number some verification is more troublesome, here with the help of the open source framework of Weixin-Java-MP this program development
.
1. Introduce weixin-java-mp.jar
< the dependency > < groupId > com. Making. Binarywang < / groupId > < artifactId > weixin - Java - mp < / artifactId > < version > 4.0.0 < / version > </dependency>
2. Configure MP
@Configuration public class WxMpConfiguration { @Autowired private WxMpProperties wxMpProperties; @Bean public WxMpService wxMpService() { WxMpServiceImpl wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); No. / / set up multiple WeChat public configuration / / wxMpService setMultiConfigStorages (); return wxMpService; } /** * This local configuration is saved locally, the production environment needs to extend itself, * * @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; }}
WXMPDefaultConfigImpl is used in a production environment. If the service is deployed in a cluster, it is best not to use WXMPDefaultConfigImpl because this keeps the configuration in memory. Consider using the WXMPredissonConfigImpl class. 2, WXMPConfigStorage in the configuration, and the above server configuration is consistent.
Server-side code validation
@Component @RestController @Slf4j public class MpEntryController { @Autowired private WxMpService wxMpService; ** * @Param timestamp * @Param timestamp * @Param nonce * @Param echoStr * @Param timestamp * @Param echoStr The value of the 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(" WeChat public/service access passed parameters signature:[{}],timestamp:[{}],nonce:[{}],echostr:[{}]", signature, timestamp, nonce, echoStr); If (StringUtls. isAnyBlank(Signature, Timestamp, Nonce, echoStr)) {log.error(" Error received, parameter is invalid, empty parameter exists "); if (StringUtls. isAnyBlank(Signature, Timestamp, Nonce, echoStr)) {log.error(" Error received, parameter is invalid, empty parameter exists "); return "error"; } boolean result = wxMpService.checkSignature(timestamp, nonce, signature); Log. Info (" WeChat public account/service account successfully accessed?[{}]", result); return result ? echoStr : "error"; }}
Note that 1, /mp/entry is our own access path, as shown in the image above.
4, validation,
2. In the WeChat configuration page, click Submit. 3. Passed verification, indicating successful access.
The WeChat server address is the URL/mp/entry
Five, reference documents
1. WeChat access
Six, code website
https://gitee.com/huan1993/wechat-development