java引⼊sdk_⼿把⼿教您开发JAVASDK-新⼿接⼊很⾼兴⼤家继续我们的开发,相信⼤家已经迫不及待了吧!
下⾯直⼊正题!
⾸先如果你已经看过的公众平台开发⽂档,如果没看过建议还是先看⼀下
如果你已经注册了或者服务号,那么你在⾼级功能,开发者模式 申请成为开发者⾥会让你填写
好了,我们就从这⾥开始吧!
操作步骤我按顺序标记了,其他的为辅助说明。
1.⾸先我们新建⼀个Java开发包WeiXinSDK
2.包路径:com.ansitech.weixin.sdk
测试的前提条件:
假如我的公众账号号为:vzhanqun
下⾯我们需要新建⼀个URL的请求地址
我们新建⼀个Servlet来验证URL的真实性,具体接⼝参考
3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java
这⾥我们主要是获取服务器法师的验证信息,具体验证代码如下
package com.ansitech.weixin.sdk;
import com.ansitech.weixin.sdk.util.SHA1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;java源代码加密
import java.util.Comparator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinUrlFilter implements Filter {
//这个Token是给开发者接⼊时填的
//可以是任意英⽂字母或数字,长度为3-32字符
private static String Token = "vzhanqun1234567890";
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("WeixinUrlFilter启动成功!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//服务器将发送GET请求到填写的URL上,这⾥需要判定是否为GET请求
boolean isGet = Method().toLowerCase().equals("get");
System.out.println("获得请求:" + Method() + " ⽅式");
if (isGet) {
//验证URL真实性
String signature = Parameter("signature");// 加密签名
String timestamp = Parameter("timestamp");// 时间戳
String nonce = Parameter("nonce");// 随机数
String echostr = Parameter("echostr");//随机字符串
List params = new ArrayList();
params.add(Token);
params.add(timestamp);
params.add(nonce);
/
/1. 将token、timestamp、nonce三个参数进⾏字典序排序
Collections.sort(params, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1pareTo(o2);
}
});
//2. 将三个参数字符串拼接成⼀个字符串进⾏sha1加密
String temp = (0) + (1) + (2)); if (temp.equals(signature)) {
}
} else {
//处理接收消息
}
}
@Override
public void destroy() {
}
}好了,不过这⾥有个SHA1算法,我这⾥也把SHA1算法的源码给贴出来吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
/*
* 公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ansitech.weixin.sdk.util;
import java.security.MessageDigest;
/**
*
Title: SHA1算法
*
* @author qsyang
*/
public final class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
/
/ 把密⽂转换成⼗六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
String();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = Instance("SHA1"); messageDigest.Bytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}5.把这个Servlet配置到l中
消息接⼊接⼝
WeixinUrlFilter
com.ansitech.weixin.sdk.WeixinUrlFilter
WeixinUrlFilter
/api/vzhanqun
好了,接⼊的开发代码已经完成。
6.下⾯就把地址URL和密钥Token填⼊到申请成为开发者模式中吧。Token:vzhanqun1234567890
假如你服务已经启动了,那么应该验证成功,成为开发者了。
如有疑问,请留⾔。
你可以关注我的:vzhanqun 微站管家

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。