为WxMpConfigStorage接口增加autoRefreshToken方法,以方便客户端设置是否自动刷新token. for #77

This commit is contained in:
Binary Wang 2016-11-18 17:32:22 +08:00
parent a37a869a30
commit 088a97221e
3 changed files with 15 additions and 1 deletions

View File

@ -100,4 +100,9 @@ public interface WxMpConfigStorage {
* @return ApacheHttpClientBuilder * @return ApacheHttpClientBuilder
*/ */
ApacheHttpClientBuilder getApacheHttpClientBuilder(); ApacheHttpClientBuilder getApacheHttpClientBuilder();
/**
* 是否自动刷新token
*/
boolean autoRefreshToken();
} }

View File

@ -270,6 +270,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
return this.apacheHttpClientBuilder; return this.apacheHttpClientBuilder;
} }
@Override
public boolean autoRefreshToken() {
return true;
}
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder; this.apacheHttpClientBuilder = apacheHttpClientBuilder;
} }

View File

@ -101,6 +101,7 @@ public class WxMpServiceImpl implements WxMpService {
if (forceRefresh) { if (forceRefresh) {
this.configStorage.expireAccessToken(); this.configStorage.expireAccessToken();
} }
if (this.configStorage.isAccessTokenExpired()) { if (this.configStorage.isAccessTokenExpired()) {
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
if (this.configStorage.isAccessTokenExpired()) { if (this.configStorage.isAccessTokenExpired()) {
@ -411,8 +412,11 @@ public class WxMpServiceImpl implements WxMpService {
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
// 强制设置wxMpConfigStorage它的access token过期了这样在下一次请求里就会刷新access token // 强制设置wxMpConfigStorage它的access token过期了这样在下一次请求里就会刷新access token
this.configStorage.expireAccessToken(); this.configStorage.expireAccessToken();
return this.execute(executor, uri, data); if(this.configStorage.autoRefreshToken()){
return this.execute(executor, uri, data);
}
} }
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data,
error); error);