如何使用SpringMVC開(kāi)發(fā)微信公眾平臺(tái)驗(yàn)證Token接口
--- 理解微信公眾平臺(tái)接入機(jī)制在研究微信公眾平臺(tái)的接入機(jī)制時(shí),我們需要關(guān)注如何實(shí)現(xiàn)與微信服務(wù)器進(jìn)行通信以驗(yàn)證Token。下面是SpringMVC實(shí)現(xiàn)代碼示例:```javapackage ;impo
---
理解微信公眾平臺(tái)接入機(jī)制
在研究微信公眾平臺(tái)的接入機(jī)制時(shí),我們需要關(guān)注如何實(shí)現(xiàn)與微信服務(wù)器進(jìn)行通信以驗(yàn)證Token。下面是SpringMVC實(shí)現(xiàn)代碼示例:
```java
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Controller
@RequestMapping("weixin")
public class WeixinController extends BaseController {
@RequestMapping(value { "signature" }, method )
public void signature(
@RequestParam(value "signature", required true) String signature,
@RequestParam(value "timestamp", required true) String timestamp,
@RequestParam(value "nonce", required true) String nonce,
@RequestParam(value "echostr", required true) String echostr,
HttpServletResponse response) throws IOException {
String[] values { GlobalConstants.WEIXIN_TOKEN, timestamp, nonce };
(values);
String value values[0] values[1] values[2];
String sign (value);
PrintWriter writer ();
if (signature.equals(sign)) {
(echostr);
} else {
("error");
}
writer.flush();
();
}
}
```
添加SHA1加密算法依賴(lài)
在使用SHA1加密算法時(shí),需引入Apache的Commons Codec依賴(lài)。在Maven中添加以下依賴(lài)項(xiàng):
```xml
```
配置微信公眾平臺(tái)開(kāi)發(fā)者中心
在微信公眾平臺(tái)的后臺(tái),需要修改開(kāi)發(fā)者中心的配置。提交后會(huì)發(fā)送驗(yàn)證參數(shù)請(qǐng)求到填寫(xiě)的URL。Nginx可能會(huì)接收到類(lèi)似請(qǐng)求: