<span style="font-family: Arial, Helvetica, sans-serif;"><strong><span style="font-size:18px;">这是主要的java逻辑:</span></strong></span>
package com.luo.util;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.luo.bean.AccessTicket;
import com.luo.bean.AccessToken;
import com.luo.bean.Signature;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Single;
import net.sf.json.JSONObject;
public class WeiXinDemo {
private static final String APPID = "wxbfe05c8668af73e0";
private static final String APPSECRET = "8352cae432e8c5bbacd88b843542afb4";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String ACCESS_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=TOKEN&type=jsapi";
public static JSONObject doGet(String url){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity != null){
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doPost(String url,String outStr){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
JSONObject jsonObject = null;
httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
try {
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
jsonObject = JSONObject.fromObject(result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static AccessToken getAccessToken(){
AccessToken token = new AccessToken();
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
JSONObject jsonObject = doGet(url);
if(jsonObject != null){
token.setToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getString("expires_in"));
}
return token;
}
public static AccessTicket getAccessTicket(String token){
AccessTicket ticket = new AccessTicket();
String url = ACCESS_TICKET_URL.replace("TOKEN",token);
JSONObject jsonObject = doGet(url);
if(jsonObject != null){
ticket.setTicket(jsonObject.getString("ticket"));
}
return ticket;
}
public static Signature getSignature(String ticket){
//获取签名signature
String noncestr = UUID.randomUUID().toString();
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
String url="http://www.999netsafe.com/entlist_1.jsp";
String str = "jsapi_ticket=" + ticket +
"&noncestr=" + noncestr +
"×tamp=" + timestamp +
"&url=" + url;
//sha1加密
String signature = SHA1(str);
Signature sig = new Signature();
sig.setSignature(signature);
sig.setNoncestr(noncestr);
sig.setTimestamp(timestamp);
return sig;
// System.out.println("noncestr=" + noncestr);
// System.out.println("timestamp=" + timestamp);
// System.out.println("signature=" + signature);
//最终获得调用微信js接口验证需要的三个参数noncestr、timestamp、signature
}
public static String SHA1(String str) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
digest.update(str.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexStr.append(0);
}
hexStr.append(shaHex);
}
return hexStr.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
AccessToken token = getAccessToken();
System.out.println("token:"+token.getToken());
AccessTicket ticket = getAccessTicket(token.getToken());
System.out.println("ticket:"+ticket.getTicket());
Signature signature = getSignature(ticket.getTicket());
System.out.println("noncestr=" + signature.getNoncestr());
System.out.println("timestamp=" + signature.getTimestamp());
System.out.println("signature=" + signature.getSignature());
}
}
<span style="font-size:18px;"><strong>三个bean</strong></span>
public class AccessTicket {
<span style="white-space:pre"> </span>private String errcode;
<span style="white-space:pre"> </span>private String errmsg;
<span style="white-space:pre"> </span>private String ticket;
<span style="white-space:pre"> </span>private String expiresIn;
<span style="white-space:pre"> </span>public String getErrcode() {
<span style="white-space:pre"> </span>return errcode;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setErrcode(String errcode) {
<span style="white-space:pre"> </span>this.errcode = errcode;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getErrmsg() {
<span style="white-space:pre"> </span>return errmsg;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setErrmsg(String errmsg) {
<span style="white-space:pre"> </span>this.errmsg = errmsg;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getTicket() {
<span style="white-space:pre"> </span>return ticket;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setTicket(String ticket) {
<span style="white-space:pre"> </span>this.ticket = ticket;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getExpiresIn() {
<span style="white-space:pre"> </span>return expiresIn;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setExpiresIn(String expiresIn) {
<span style="white-space:pre"> </span>this.expiresIn = expiresIn;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;">public class AccessToken {
<span style="white-space:pre"> </span>private String token;
<span style="white-space:pre"> </span>private String expiresIn;
<span style="white-space:pre"> </span>public String getToken() {
<span style="white-space:pre"> </span>return token;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setToken(String token) {
<span style="white-space:pre"> </span>this.token = token;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getExpiresIn() {
<span style="white-space:pre"> </span>return expiresIn;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setExpiresIn(String expiresIn) {
<span style="white-space:pre"> </span>this.expiresIn = expiresIn;
<span style="white-space:pre"> </span>} </span>
public class Signature {
<span style="white-space:pre"> </span>private String signature;
<span style="white-space:pre"> </span>private String noncestr;
<span style="white-space:pre"> </span>private String timestamp;
<span style="white-space:pre"> </span>public String getSignature() {
<span style="white-space:pre"> </span>return signature;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setSignature(String signature) {
<span style="white-space:pre"> </span>this.signature = signature;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getNoncestr() {
<span style="white-space:pre"> </span>return noncestr;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setNoncestr(String noncestr) {
<span style="white-space:pre"> </span>this.noncestr = noncestr;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getTimestamp() {
<span style="white-space:pre"> </span>return timestamp;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setTimestamp(String timestamp) {
<span style="white-space:pre"> </span>this.timestamp = timestamp;
<span style="white-space:pre"> </span>}
添加上相应的jar包就可以
<img src="https://img-blog.csdn.net/20160922161410839?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />