/**
*
* @author Administrator
*
*/
public class AccessToken {
/** * access_token*/
private final String access_token;
/** * access_token time*/
private final long expire_in; // access_token有效时间,单位为妙
/**
*
* @param access_token access_token
* @param expire_in expire_in
*/
public AccessToken(String access_token, long expire_in)
{
this.access_token = access_token;
this.expire_in = expire_in;
}
public String getAccess_token() {
return access_token;
}
public long getExpire_in() {
return expire_in;
}
}
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
/**
* 智慧校园 AccessTokenCache
* @author Administrator
*
*/
public class AccessTokenCache {
/**
* 记录日志
*/
private static Logger LOGGER = Logger.getLogger(AccessTokenCache.class);
/** * accessToken */
private static volatile AccessToken ACCESSTOKEN;
/** * RefreshThread is run or stop */
public static volatile boolean STARTED = false;
/** * get accessToken url*/
private static final String URL = System.getProperty("smp.smartcampus.accesstokenurl",
"");
/** * grant_type */
private static final String GRANTTYPE = System.getProperty("smp.smartcampus.granttype",
"");
/** * client_id */
private static final String CLIENTID = System.getProperty("smp.smartcampus.clientid",
"");
/** * client_secret */
private static final String CLIENTSECRET = System.getProperty("smp.smartcampus.clientsecret",
"");
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
* @return AccessToken
*/
public static AccessToken getAccessToken() {
if (ACCESSTOKEN == null) {
ACCESSTOKEN = getToken();
}
//获取到的Accesstoken不为空, 并且更新线程未启动
if (ACCESSTOKEN != null && !STARTED) {
startRefreshThread();
}
return ACCESSTOKEN;
}
/**
*
* Description: 单独获取AccessToken 不启用线程
* Create Time: 2019-7-17
* @author Administrator
* @return AccessToken
*/
public static AccessToken getSingleAccessToken() {
ACCESSTOKEN = getToken();
return ACCESSTOKEN;
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
* @param token token
*/
public static void refreshAccessToken(AccessToken token) {
if (token != null) {
ACCESSTOKEN = token;
} else {
//TODO 记录日志
LOGGER.error("get access_token failed");
}
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
*/
public static void stopRefreshThread() {
STARTED = false;
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
*/
public static void startRefreshThread() {
if (STARTED) {
return;
}
STARTED = true;
//启动定时更新线程
new Thread(new AccessTokenRefreshThread()).start();
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
*/
public static void refreshAccessToken() {
AccessToken token = getToken();
refreshAccessToken(token);
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
* @return AccessToken
*/
private static AccessToken getToken() {
try {
return getTokenByCilent();
} catch (Exception e) {
LOGGER.error("智慧校园 获取access_token 失败", e);
}
return null;
}
/**
*
* Description:
* Create Time: 2019-7-15
* @author Administrator
* @return AccessToken
*/
private static AccessToken getTokenByCilent() {
AccessToken accessToken = null;
String response = "";
String url = String.format("%s?grant_type=%s&client_id=%s&client_secret=%s",
URL, GRANTTYPE, CLIENTID, CLIENTSECRET);
int i=1;
HttpGet post = new HttpGet(url);
HttpClient client = null;
HttpResponse httpResponse =null;
client = new DefaultHttpClient();
try {
httpResponse = client.execute(post);
response = EntityUtils.toString(httpResponse.getEntity());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
/**
* {
"access_token": "AT-1-y2bUvRKfIP8VGDBGZ6t7JhZ4zeJy0gDf",
"token_type": "bearer",
"expires_in": 28800
}
*/
JSONObject json = JSONObject.fromObject(response);
if (json.has("access_token")) {
String access_token = json.getString("access_token");
String expires_in = json.getString("expires_in");
accessToken = new AccessToken("Bearer " + access_token, Integer.valueOf(expires_in));
LOGGER.error("智慧校园 获取access_token 成功");
} else {
LOGGER.error("智慧校园 获取access_token 失败,返回结果为:" + json);
}
return accessToken;
}
public static void main(String[] args) throws InterruptedException {
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
System.out.println(AccessTokenCache.getAccessToken().getAccess_token());
}
/**
* AccessTokenRefreshThread
* @author Administrator
*
*/
static class AccessTokenRefreshThread implements Runnable {
public void run() {
while (STARTED)
{
try {
if (null != ACCESSTOKEN) {
// Thread.sleep((10) * 1000);
Thread.sleep((ACCESSTOKEN.getExpire_in() / 2 ) * 1000); // 休眠14400秒 4个小时
} else {
Thread.sleep(60 * 1000); // 如果access_token为null,60秒后再获取
}
} catch (InterruptedException e) {
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e1) {
LOGGER.error("AccessTokenRefreshThread", e);
}
}
//更新Accesstoken
refreshAccessToken();
}
}
}
}