java版本jssdk、验证签名demo(Servlet)
开发背景
1. 最近在学习java,所以从⽹上看了⼀些别⼈的代码,再加⼊了⾃⼰的⼀些理解,主要是担⼼之后会忘记,所以写下⼀篇东西记录⼀下
思路。
⼀、获取acess_token以及jsapiTicket
1. 新建⼀个类TokenThread,实现线程的Runnable接⼝,代码如下:
TokenThread
package util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
le.gson.Gson;
import model.AccessToken;
import model.JsapiTicket;
/**
* Created by xu on 2017/7/10.
*/
public class TokenThread implements Runnable {
public static String appId = "xxx";
public static String appSecret= "xxx";
public static AccessToken accessToken = null;
public static JsapiTicket jsapiTicket = null;
public final static String js_api_ticket_url = "api.weixin.qq/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
@Override
public void run() {
while (true) {
try {
accessToken = AccessToken();
jsapiTicket = JsapiTicket();
//获取到accessToken后,休眠7000秒,保证不会重复的获取,超过了每天的最⼤次数
if (null != accessToken) {
System.out.AccessToken());
System.out.JsapiTicket());
Thread.sleep(7000 * 1000); //获取到access_token 休眠7000秒
} else {
Thread.sleep(1000 * 3); //获取的access_token为空休眠3秒
}
} catch (Exception e) {
System.out.println("发⽣异常:" + e.getMessage());
e.printStackTrace();
try {
Thread.sleep(1000 * 10); //发⽣异常休眠1秒
} catch (Exception e1) {
}
}
}
/**
* 获取access_token
* @return
*/
private AccessToken getAccessToken(){
NetWorkHelper netHelper = new NetWorkHelper();
String Url = String.format("api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",this.appId,this.appSecret);        String result = HttpsResponse(Url,"");
System.out.println(result);
//Writer().println(result);
JSONObject json = JSONObject.parseObject(result);
System.out.println(json);
AccessToken token = new AccessToken();
token.String("access_token"));
token.Integer("expires_in"));
return token;
}
/**
* 获取jsapi_ticket
* @return
*/
private JsapiTicket getJsapiTicket(){
NetWorkHelper netHelper = new NetWorkHelper();
String Url = js_api_place("ACCESS_TOKEN",AccessToken().getAccessToken());
String result = HttpsResponse(Url,"");
System.out.println(result);
JSONObject json = JSONObject.parseObject(result);
System.out.println(json);
JsapiTicket jsapiTicket = new JsapiTicket();
jsapiTicket.String("ticket"));
jsapiTicket.Integer("expires_in"));
return jsapiTicket;
}
}
这⾥⽤到了两个⼯具类NetWorkHelper,发起⽹络请求⽹上有封装好的,我做⼀下搬运⼯:
NetWorkHelper
package util;
import javax.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.URL;
import Certificate;
import CertificateException;
import X509Certificate;
/**
* Created by xu on 2017/7/10.
public class NetWorkHelper {
public String getHttpsResponse(String hsUrl,String requestMethod) {
URL url;
InputStream is = null;
String resultData = "";
try {
url = new URL(hsUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();            TrustManager[] tm = {xtm};
SSLContext ctx = Instance("TLS");
ctx.init(null, tm, null);
con.SocketFactory());
con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
con.setDoInput(true); //允许输⼊流,即允许下载
//在android中必须将此项设置为false
con.setDoOutput(false); //允许输出流,即允许上传
con.setUseCaches(false); //不使⽤缓冲
if(null!=requestMethod && !requestMethod.equals("")) {
con.setRequestMethod(requestMethod); //使⽤指定的⽅式
}
else{
con.setRequestMethod("GET"); //使⽤get请求
}
is = InputStream(); //获取输⼊流,此时才真正建⽴链接
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufferReader = new BufferedReader(isr);
String inputLine = "";
while ((inputLine = adLine()) != null) {
resultData += inputLine + "\n";
}
System.out.println(resultData);
Certificate[] certs = ServerCertificates();
int certNum = 1;
for (Certificate cert : certs) {
X509Certificate xcert = (X509Certificate) cert;
}
} catch (Exception e) {
e.printStackTrace();
}
return resultData;
}
X509TrustManager xtm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
};
}
上⾯还引⼊了阿⾥巴巴的json解析包import com.alibaba.fastjson.JSONObject;⾃⾏上⽹搜索就好。
上⾯的获取accessToken的过程中,new了⼀个AccessToken和JsapiTicket对象,我把她们放进了⼀个model类⾥。
AccessToken
package model;
/**
* Created by xu on 2017/7/10.
*/
public class AccessToken {
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public int getExpiresin() {
return expiresin;
}
public void setExpiresin(int expiresin) {
}
private String accessToken;
private int expiresin;
}
JsapiTicket
package model;
/**
* Created by xu on 2017/7/12.
*/
public class JsapiTicket {
private String jsapiTicket;
private int expiresin;
public String getJsapiTicket() {
android获取真正的签名
return jsapiTicket;
}
public void setJsapiTicket(String jsapiTicket) {
this.jsapiTicket = jsapiTicket;
}
public int getExpiresin() {
return expiresin;
}
public void setExpiresin(int expiresin) {
}
}
新建⼀个HttpServlet,AccessTokenServlet,⽤来执⾏获取,初始化的过程中执⾏,这⾥需要配置l

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