前面几篇文章一直都在说微信公众平台的开发准备工作,那么从这篇开始我们就将正式的进入java微信公众平台开发的整个流程,那么这篇我们开始聊聊如何将我们的服务端和微信公众平台对接!
(一)接入流程解析
在我们的开发过程中无论如何最好的参考工具当然是我们的官方文档了:http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
通过上面我们可以看出其中接入微信公众平台开发,开发者需要按照如下步骤完成:
- 填写服务器配置
- 验证服务器地址的有效性
- 依据接口文档实现业务逻辑
按照上面的逻辑可能是填写服务器配置信息是在第一步,但是我们在真实的开发过程中往往都是先做第二步【编写代码实现验证服务器地址的有效性】,因为没有第二步的完成第一步的配置是不能达到任何效果的!
(二)验证服务器有效性代码编写
按照开发文档我们知道我们的应用服务器需要接受微信服务器的get请求,其中包含四个参数(signature、timestamp、nonce、echostr)然后通过校验方式校验服务器的可靠性,校验方式如下:
- 将token、timestamp、nonce三个参数进行字典序排序
- 将三个参数字符串拼接成一个字符串进行sha1加密
- 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
①我在这里写了一个工具类去实现其中的前两步,将三个参数排序并返回sha1加密后的字符串,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package com.cuiyongzhi.wechat.util;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
import java.util.arrays;
/**
* classname: signutil
* @description: 请求校验工具类
* @author dapengniao
* @date 2016年3月4日 下午6:25:41
*/
public class signutil {
// 与接口配置信息中的token要一致
private static string token = "dapengniaowechat";
/**
* 验证签名
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean checksignature(string signature, string timestamp, string nonce) {
string[] arr = new string[] { token, timestamp, nonce };
// 将token、timestamp、nonce三个参数进行字典序排序
arrays.sort(arr);
stringbuilder content = new stringbuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
messagedigest md = null;
string tmpstr = null;
try {
md = messagedigest.getinstance("sha-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.tostring().getbytes());
tmpstr = bytetostr(digest);
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpstr != null ? tmpstr.equals(signature.touppercase()) : false;
}
/**
* 将字节数组转换为十六进制字符串
* @param bytearray
* @return
*/
private static string bytetostr(byte[] bytearray) {
string strdigest = "";
for (int i = 0; i < bytearray.length; i++) {
strdigest += bytetohexstr(bytearray[i]);
}
return strdigest;
}
/**
* 将字节转换为十六进制字符串
* @param mbyte
* @return
*/
private static string bytetohexstr(byte mbyte) {
char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char[] temparr = new char[2];
temparr[0] = digit[(mbyte >>> 4) & 0x0f];
temparr[1] = digit[mbyte & 0x0f];
string s = new string(temparr);
return s;
}
}
|
②将我们的工具类应用到我们的服务器验证过程中,这里我新建一个controller为wechatsecurity,实现同一个get用于接收参数和返回验证参数,简单代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package com.cuiyongzhi.wechat.controller;
import java.io.printwriter;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.log4j.logger;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import com.cuiyongzhi.wechat.util.signutil;
@controller
@requestmapping("/wechat")
public class wechatsecurity {
private static logger logger = logger.getlogger(wechatsecurity.class);
/**
*
* @description: 用于接收get参数,返回验证参数
* @param @param request
* @param @param response
* @param @param signature
* @param @param timestamp
* @param @param nonce
* @param @param echostr
* @author dapengniao
* @date 2016年3月4日 下午6:20:00
*/
@requestmapping(value = "security", method = requestmethod.get)
public void doget(
httpservletrequest request,
httpservletresponse response,
@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) {
try {
if (signutil.checksignature(signature, timestamp, nonce)) {
printwriter out = response.getwriter();
out.print(echostr);
out.close();
} else {
logger.info("这里存在非法请求!");
}
} catch (exception e) {
logger.error(e, e);
}
}
@requestmapping(value = "security", method = requestmethod.post)
// post方法用于接收微信服务端消息
public void dopost() {
system.out.println("这是post方法!");
}
}
|
那么到这里我们的服务器验证的代码就基本完成了,下面我们就进入验证过程!
(三)服务器验证
这里我用来验证的是我的个人公众号【崔用志】,如果大家有兴趣可以搜索看到的,通过微博认证的一个私人号,当然有想法在这里我们也是可以一起交流的,验证方法如下图:
点击【提交】成功之后如下图所示:
点击图中【启用】即可,那么到这里我们的服务器接入配置就完成了,【下一篇我们将讲述如何接收消息并进行消息处理】,感谢你的翻阅,如有疑问可以留言讨论!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 106
-
2025-05-25 46
-
2025-05-27 58
-
2025-05-27 33
-
2025-05-25 37







