最初将access_token放到数据库中,每次获取时判断是否过期。可以解决access_token的有效期及访问次数问题。但是最近需要将公众号与系统做接口,取消公众号的数据库,此时再将access_token等放到系统的数据库中就不合适了。
受益于薛喜麟的这篇文章:微信处理access_token全局缓存的问题,实现了用单例模式来缓存access_token。
记一下,供以后查用。
mark一下自己的代码结构:
public class tokensingleton {
//缓存accesstoken 的map ,map中包含 一个accesstoken 和 缓存的时间戳
private map<string, string> map = new hashmap<>();
private tokensingleton() {
}
private static tokensi
ngleton single = null;
// 静态工厂方法
public static tokensingleton getinstance() {
if (single == null) {
single = new tokensingleton();
}
return single;
}
public map<string, string> getmap() {
string time = map.get("time");
string accesstoken = map.get("access_token");
long nowdate = new date().gettime();
if (accesstoken != null && time != null && nowdate - long.parselong(time) < 6000*1000) {
system.out.println("accesstoken存在,且没有超时 , 返回单例");
} else {
system.out.println("accesstoken 超时 , 或者不存在 , 重新获取");
string access_token=jssdkutil.getaccesstoken();
//这里是直接调用微信的api去直接获取 accesstoken 和jsapi_ticket 获取
string jsapi_token = jssdkutil.getticket(access_token);
//"获取jsapi_token";
map.put("time", nowdate + "");
map.put("access_token", access_token);
map.put("jsapi_token", jsapi_token);
}
return map;
}
public void setmap(map<string, string> map) {
this.map = map;
}
public static tokensingleton getsingle() {
return single;
}
public static void setsingle(tokensingleton single) {
tokensingleton.single = single;
}
}
另外一个工具类jssdkutil.java截取了部分:
public static string getsignature(string accesstoken,string jsapi_ticket,string noncestr,string timestamp,string url){
system.out.println("accesstoken:"+accesstoken+"\njsapi_ticket:"+jsapi_ticket+"\n时间戳:"+timestamp+"\n随机字符串:"+noncestr+"\nurl:"+url);
//5、将参数排序并拼接字符串
string str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+url;
//6、将字符串进行sha1加密
string signature =sha1(str);
system.out.println("参数:"+str+"\n签名:"+signature);
return signature;
}
public static string getaccesstoken() {
string wx_appid = getproperties("wx_appid");
string wx_appsecret = getproperties("wx_appsecret");
string access_token = "";
string grant_type = "client_credential";//获取access_token填写client_credential
string appid=wx_appid;//第三方用户唯一凭证
string secret=wx_appsecret;//第三方用户唯一凭证密钥,即appsecret
//这个url链接地址和参数皆不能变
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appid+"&secret="+secret;
try {
url urlget = new url(url);
httpurlconnection http = (httpurlconnection) urlget.openconnection();
http.setrequestmethod("get"); // 必须是get方式请求
http.setrequestproperty("content-type","application/x-www-form-urlencoded");
http.setdooutput(true);
http.setdoinput(true);
system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 连接超时30秒
system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 读取超时30秒
http.connect();
inputstream is = http.getinputstream();
int size = is.available();
byte[] jsonbytes = new byte[size];
is.read(jsonbytes);
string message = new string(jsonbytes, "utf-8");
jsonobject demojson = jsonobject.fromobject(message);
system.out.println("json字符串:"+demojson);
access_token = demojson.getstring("access_token");
is.close();
} catch (exception e) {
e.printstacktrace();
}
return access_token;
}
public static string getticket(string access_token) {
string ticket = null;
string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变
try {
url urlget = new url(url);
httpurlconnection http = (httpurlconnection) urlget.openconnection();
http.setrequestmethod("get"); // 必须是get方式请求
http.setrequestproperty("content-type","application/x-www-form-urlencoded");
http.setdooutput(true);
http.setdoinput(true);
system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 连接超时30秒
system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 读取超时30秒
http.connect();
inputstream is = http.getinputstream();
int size = is.available();
byte[] jsonbytes = new byte[size];
is.read(jsonbytes);
string message = new string(jsonbytes, "utf-8");
jsonobject demojson = jsonobject.fromobject(message);
system.out.println("json字符串:"+demojson);
ticket = demojson.getstring("ticket");
is.close();
} catch (exception e) {
e.printstacktrace();
}
return ticket;
}
public static string sha1(string decript) {
try {
messagedigest digest = java.security.messagedigest.getinstance("sha-1");
digest.update(decript.getbytes());
byte messagedigest[] = digest.digest();
// create hex string
stringbuffer hexstring = new stringbuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messagedigest.length; i++) {
string shahex = integer.tohexstring(messagedigest[i] & 0xff);
if (shahex.length() < 2) {
hexstring.append(0);
}
hexstring.append(shahex);
}
return hexstring.tostring();
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
}
return "";
}
相关推荐:
浅析mysql内存的使用说明(全局缓存+线程缓存)_mysql
以上就是单例模式如何解决access_token全局缓存问题的详细内容。