进阶学习开发JAVA微信SDK的3个步骤
白羽 2018-06-12 来源 :网络 阅读 568 评论 0

摘要:本文将带你了解开发JAVA微信SDK的3个步骤,希望本文对大家学微信有所帮助。


 


操作步骤我按顺序标记了,其他的为辅助说明。

1.首先我们新建一个Java开发包WeiXinSDK

2.包路径:com.ansitech.weixin.sdk

测试的前提条件:

假如我的公众账号微信号为:zhizuobiao

我的服务器地址为://www.zhizuobiao.com/

下面我们需要新建一个URL的请求地址

我们新建一个Servlet来验证URL的真实性,具体接口参考

//mp.weixin.qq.com/wiki/index.php?title=接入指南

3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java

这里我们主要是获取微信服务器法师的验证信息,具体验证代码如下

[

java] view plain copy
1. package com.ansitech.weixin.sdk;  
2.   
3. import com.ansitech.weixin.sdk.util.SHA1;  
4. import java.io.IOException;  
5. import java.util.ArrayList;  
6. import java.util.Collections;  
7. import java.util.Comparator;  
8. import java.util.List;  
9. import javax.servlet.Filter;  
10. import javax.servlet.FilterChain;  
11. import javax.servlet.FilterConfig;  
12. import javax.servlet.ServletException;  
13. import javax.servlet.ServletRequest;  
14. import javax.servlet.ServletResponse;  
15. import javax.servlet.http.HttpServletRequest;  
16. import javax.servlet.http.HttpServletResponse;  
17.   
18. public class WeixinUrlFilter implements Filter {  
19.   
20.     //这个Token是给微信开发者接入时填的  
21.     //可以是任意英文字母或数字,长度为3-32字符  
22.     private static String Token = "vzhanqun1234567890";  
23.   
24.     @Override  
25.     public void init(FilterConfig config) throws ServletException {  
26.         System.out.println("WeixinUrlFilter启动成功!");  
27.     }  
28.   
29.     @Override  
30.     public void doFilter(ServletRequest req, ServletResponse res,  
31.             FilterChain chain) throws IOException, ServletException {  
32.         HttpServletRequest request = (HttpServletRequest) req;  
33.         HttpServletResponse response = (HttpServletResponse) res;  
34.         //微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求  
35.         boolean isGet = request.getMethod().toLowerCase().equals("get");  
36.         System.out.println("获得微信请求:" + request.getMethod() + " 方式");  
37.         if (isGet) {  
38.             //验证URL真实性  
39.             String signature = request.getParameter("signature");// 微信加密签名  
40.             String timestamp = request.getParameter("timestamp");// 时间戳  
41.             String nonce = request.getParameter("nonce");// 随机数  
42.             String echostr = request.getParameter("echostr");//随机字符串  
43.             List<String> params = new ArrayList<String>();  
44.             params.add(Token);  
45.             params.add(timestamp);  
46.             params.add(nonce);  
47.             //1. 将token、timestamp、nonce三个参数进行字典序排序  
48.             Collections.sort(params, new Comparator<String>() {  
49.                 @Override  
50.                 public int compare(String o1, String o2) {  
51.                     return o1.compareTo(o2);  
52.                 }  
53.             });  
54.             //2. 将三个参数字符串拼接成一个字符串进行sha1加密  
55.             String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));  
56.             if (temp.equals(signature)) {  
57.                 response.getWriter().write(echostr);  
58.             }  
59.         } else {  
60.             //处理接收消息  
61.         }  
62.     }  
63.   
64.     @Override  
65.     public void destroy() {  
66.           
67.     }  
68. }  
好了,不过这里有个SHA1算法,我这里也把SHA1算法的源码给贴出来吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
[java] view plain copy
1. /* 
2.  * 微信公众平台(JAVA) SDK 
3.  * 
4.  * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. 
5.  * //www.ansitech.com/weixin/sdk/ 
6.  * 
7.  * Licensed under the Apache License, Version 2.0 (the "License"); 
8.  * you may not use this file except in compliance with the License. 
9.  * You may obtain a copy of the License at 
10.  * 
11.  *      //www.apache.org/licenses/LICENSE-2.0 
12.  * 
13.  * Unless required by applicable law or agreed to in writing, software 
14.  * distributed under the License is distributed on an "AS IS" BASIS, 
15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
16.  * See the License for the specific language governing permissions and 
17.  * limitations under the License. 
18.  */  
19. package com.ansitech.weixin.sdk.util;  
20.   
21. import java.security.MessageDigest;  
22.   
23. /** 
24.  * <p>Title: SHA1算法</p> 
25.  * 
26.  * @author qsyang<yangqisheng274@163.com> 
27.  */  
28. public final class SHA1 {  
29.   
30.     private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',  
31.                            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};  
32.   
33.     /** 
34.      * Takes the raw bytes from the digest and formats them correct. 
35.      * 
36.      * @param bytes the raw bytes from the digest. 
37.      * @return the formatted bytes. 
38.      */  
39.     private static String getFormattedText(byte[] bytes) {  
40.         int len = bytes.length;  
41.         StringBuilder buf = new StringBuilder(len * 2);  
42.         // 把密文转换成十六进制的字符串形式  
43.         for (int j = 0; j < len; j++) {  
44.             buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);  
45.             buf.append(HEX_DIGITS[bytes[j] & 0x0f]);  
46.         }  
47.         return buf.toString();  
48.     }  
49.   
50.     public static String encode(String str) {  
51.         if (str == null) {  
52.             return null;  
53.         }  
54.         try {  
55.             MessageDigest messageDigest = MessageDigest.getInstance("SHA1");  
56.             messageDigest.update(str.getBytes());  
57.             return getFormattedText(messageDigest.digest());  
58.         } catch (Exception e) {  
59.             throw new RuntimeException(e);  
60.         }  
61.     }  
62. }  
5.把这个Servlet配置到web.xml中
[html] view plain copy
1. <filter>  
2.     <description>微信消息接入接口</description>  
3.     <filter-name>WeixinUrlFilter</filter-name>  
4.     <filter-class>com.ansitech.weixin.sdk.WeixinUrlFilter</filter-class>  
5. </filter>  
6. <filter-mapping>  
7.     <filter-name>WeixinUrlFilter</filter-name>  
8.     <url-pattern>/api/vzhanqun</url-pattern>  
9. </filter-mapping>  
好了,接入的开发代码已经完成。
6.下面就把地址URL和密钥Token填入到微信申请成为开发者模式中吧。
URL://www.zhizuobiao.com/api/zhizuobiao
Token:zhizuobiao1234567890

假如你服务已经启动了,那么应该验证成功,成为开发者了。

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程