mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	为WxMpConfigStorage接口增加autoRefreshToken方法,以方便客户端设置是否自动刷新token. for #77
This commit is contained in:
		| @ -100,4 +100,9 @@ public interface WxMpConfigStorage { | |||||||
|    * @return ApacheHttpClientBuilder |    * @return ApacheHttpClientBuilder | ||||||
|    */ |    */ | ||||||
|   ApacheHttpClientBuilder getApacheHttpClientBuilder(); |   ApacheHttpClientBuilder getApacheHttpClientBuilder(); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 是否自动刷新token | ||||||
|  |    */ | ||||||
|  |   boolean autoRefreshToken(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -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; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -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(); | ||||||
|  |         if(this.configStorage.autoRefreshToken()){ | ||||||
|           return this.execute(executor, uri, data); |           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); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang