针对AccessToken,jsapi ticket添加强制刷新的功能

This commit is contained in:
Daniel Qian
2015-01-20 14:30:44 +08:00
parent ae99bcdb63
commit 5c7448f212
4 changed files with 21 additions and 10 deletions

View File

@ -51,11 +51,11 @@ public interface WxCpService {
* 程序员在非必要情况下尽量不要主动调用此方法 * 程序员在非必要情况下尽量不要主动调用此方法
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token
* </pre> * </pre>
* * @param forceRefresh 强制刷新
* @return * @return
* @throws me.chanjar.weixin.common.exception.WxErrorException * @throws me.chanjar.weixin.common.exception.WxErrorException
*/ */
public String getAccessToken() throws WxErrorException; public String getAccessToken(boolean forceRefresh) throws WxErrorException;
/** /**
* <pre> * <pre>

View File

@ -71,7 +71,10 @@ public class WxCpServiceImpl implements WxCpService {
execute(new SimpleGetRequestExecutor(), url, null); execute(new SimpleGetRequestExecutor(), url, null);
} }
public String getAccessToken() throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
wxCpConfigStorage.expireAccessToken();
}
if (wxCpConfigStorage.isAccessTokenExpired()) { if (wxCpConfigStorage.isAccessTokenExpired()) {
synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) { synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) {
if (wxCpConfigStorage.isAccessTokenExpired()) { if (wxCpConfigStorage.isAccessTokenExpired()) {
@ -359,7 +362,7 @@ public class WxCpServiceImpl implements WxCpService {
* @throws WxErrorException * @throws WxErrorException
*/ */
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
String accessToken = getAccessToken(); String accessToken = getAccessToken(false);
String uriWithAccessToken = uri; String uriWithAccessToken = uri;
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;

View File

@ -40,10 +40,11 @@ public interface WxMpService {
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token
* </pre> * </pre>
* @param forceRefresh 强制刷新
* @return * @return
* @throws me.chanjar.weixin.common.exception.WxErrorException * @throws me.chanjar.weixin.common.exception.WxErrorException
*/ */
public String getAccessToken() throws WxErrorException; public String getAccessToken(boolean forceRefresh) throws WxErrorException;
/** /**
* <pre> * <pre>
@ -52,10 +53,11 @@ public interface WxMpService {
* *
* 详情请见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95 * 详情请见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
* </pre> * </pre>
* @param forceRefresh 强制刷新
* @return * @return
* @throws WxErrorException * @throws WxErrorException
*/ */
public String getJsapiTicket() throws WxErrorException; public String getJsapiTicket(boolean forceRefresh) throws WxErrorException;
/** /**
* <pre> * <pre>

View File

@ -69,7 +69,10 @@ public class WxMpServiceImpl implements WxMpService {
} }
} }
public String getAccessToken() throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
wxMpConfigStorage.expireAccessToken();
}
if (wxMpConfigStorage.isAccessTokenExpired()) { if (wxMpConfigStorage.isAccessTokenExpired()) {
synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) { synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) {
if (wxMpConfigStorage.isAccessTokenExpired()) { if (wxMpConfigStorage.isAccessTokenExpired()) {
@ -104,7 +107,10 @@ public class WxMpServiceImpl implements WxMpService {
} }
public String getJsapiTicket() throws WxErrorException { public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
wxMpConfigStorage.expireJsapiTicket();
}
if (wxMpConfigStorage.isJsapiTicketExpired()) { if (wxMpConfigStorage.isJsapiTicketExpired()) {
synchronized (GLOBAL_JSAPI_TICKET_REFRESH_LOCK) { synchronized (GLOBAL_JSAPI_TICKET_REFRESH_LOCK) {
if (wxMpConfigStorage.isJsapiTicketExpired()) { if (wxMpConfigStorage.isJsapiTicketExpired()) {
@ -122,7 +128,7 @@ public class WxMpServiceImpl implements WxMpService {
} }
public String createJsapiSignature(String timestamp, String noncestr, String url) throws WxErrorException { public String createJsapiSignature(String timestamp, String noncestr, String url) throws WxErrorException {
String jsapiTicket = getJsapiTicket(); String jsapiTicket = getJsapiTicket(false);
try { try {
return SHA1.genWithAmple( return SHA1.genWithAmple(
"jsapi_ticket=" + jsapiTicket, "jsapi_ticket=" + jsapiTicket,
@ -436,7 +442,7 @@ public class WxMpServiceImpl implements WxMpService {
* @throws WxErrorException * @throws WxErrorException
*/ */
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
String accessToken = getAccessToken(); String accessToken = getAccessToken(false);
String uriWithAccessToken = uri; String uriWithAccessToken = uri;
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;