Browse Source

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

master
Binary Wang 8 years ago
parent
commit
088a97221e
3 changed files with 15 additions and 1 deletions
  1. +5
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java
  2. +5
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java
  3. +5
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

+ 5
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java View File

@@ -100,4 +100,9 @@ public interface WxMpConfigStorage {
* @return ApacheHttpClientBuilder
*/
ApacheHttpClientBuilder getApacheHttpClientBuilder();

/**
* 是否自动刷新token
*/
boolean autoRefreshToken();
}

+ 5
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java View File

@@ -270,6 +270,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
return this.apacheHttpClientBuilder;
}

@Override
public boolean autoRefreshToken() {
return true;
}

public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}


+ 5
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java View File

@@ -101,6 +101,7 @@ public class WxMpServiceImpl implements WxMpService {
if (forceRefresh) {
this.configStorage.expireAccessToken();
}

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

if (error.getErrorCode() != 0) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data,
error);


Loading…
Cancel
Save