1, Background
Recently, for some reasons, it has involved the development of WeChat official account. Here, we simply record the access of WeChat official account.
2, Preconditions
1. You need a domain name that can be accessed on the Internet
2. For local development, you need to penetrate the Intranet environment to the Internet for access.
3, official account is needed.
be careful:
1. Intranet penetration and extranet domain names can be purchased through natapp.
3, Through natapp intranet penetration and domain name purchase
1. Purchase tunnel
Here, according to your own situation, buy a tunnel suitable for you.
Later, we will map our own local port to a website accessible from the Internet.
2. Buy a secondary domain name (if you have one, you can ignore it)
be careful:
1. Buy a secondary domain name here. If you have a domain name, you don't need to buy it.
3. Bind the domain name to the purchased tunnel
4. Download natapp client
https://natapp.cn/#download
Here, you need to download the corresponding client according to your own operating system.
5. Start natapp
The value of authtoken here is the value of the tunnel we purchased.
4, Write wechat server access verification
1. Fill in server configuration
Path: Development - > basic configuration
2. Access verification server code writing
Wechat server address URL: this can be written down first, and the following will tell you where to configure it
Our own access to WeChat official account is somewhat troublesome. We are developing it with the help of open source framework weixin-java-mp.
1. Introduce Weixin Java MP jar
<dependency> <groupId>com.github.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()); // Configuration of multiple WeChat official account // wxMpService.setMultiConfigStorages(); return wxMpService; } /** * The configuration of this place is saved locally. The production environment needs to be expanded by itself and can be saved in Redis, etc * * @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; } }
be careful
1. WxMpConfigStorage is a class in the production environment. If the service is deployed in a cluster, it is better not to use WxMpDefaultConfigImpl, because this is to save the configuration in memory. You can consider using the wxmepressisonconfigimpl class.
2. The configuration in WxMpConfigStorage is consistent with the server configuration above.
3. Server side code verification
@Component @RestController @Slf4j public class MpEntryController { @Autowired private WxMpService wxMpService; /** * Wechat access * * @param signature autograph * @param timestamp time stamp * @param nonce random number * @param echoStr Random string * @return The value of echoStr will be returned after successful access. Otherwise, it will be returned casually */ @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 official account/Parameters of service number access transmission signature:[{}],timestamp:[{}],nonce:[{}],echostr:[{}]", signature, timestamp, nonce, echoStr); if (StringUtils.isAnyBlank(signature, timestamp, nonce, echoStr)) { log.error("Wechat authentication information received,invalid parameter,There are empty parameters"); return "error"; } boolean result = wxMpService.checkSignature(timestamp, nonce, signature); log.info("WeChat official account/Service number access succeeded?[{}]", result); return result ? echoStr : "error"; } }
be careful
1. / mp/entry is our own access path, which is consistent with that saved in the figure above.
4. Verify
1. Start our web project
2. On the wechat configuration page, click Submit.
3. If the verification is passed, it indicates that the access is successful.
The wechat server address URL is / mp/entry