mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	修复代理没有设置时会存在的问题
This commit is contained in:
		| @ -65,7 +65,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|    */ |    */ | ||||||
|   protected final Object globalJsapiTicketRefreshLock = new Object(); |   protected final Object globalJsapiTicketRefreshLock = new Object(); | ||||||
|  |  | ||||||
|   protected WxCpConfigStorage wxCpConfigStorage; |   protected WxCpConfigStorage configStorage; | ||||||
|  |  | ||||||
|   protected CloseableHttpClient httpClient; |   protected CloseableHttpClient httpClient; | ||||||
|  |  | ||||||
| @ -81,7 +81,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|   @Override |   @Override | ||||||
|   public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) { |   public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) { | ||||||
|     try { |     try { | ||||||
|       return SHA1.gen(this.wxCpConfigStorage.getToken(), timestamp, nonce, data) |       return SHA1.gen(this.configStorage.getToken(), timestamp, nonce, data) | ||||||
|           .equals(msgSignature); |           .equals(msgSignature); | ||||||
|     } catch (Exception e) { |     } catch (Exception e) { | ||||||
|       return false; |       return false; | ||||||
| @ -102,14 +102,14 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|   @Override |   @Override | ||||||
|   public String getAccessToken(boolean forceRefresh) throws WxErrorException { |   public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||||
|     if (forceRefresh) { |     if (forceRefresh) { | ||||||
|       this.wxCpConfigStorage.expireAccessToken(); |       this.configStorage.expireAccessToken(); | ||||||
|     } |     } | ||||||
|     if (this.wxCpConfigStorage.isAccessTokenExpired()) { |     if (this.configStorage.isAccessTokenExpired()) { | ||||||
|       synchronized (this.globalAccessTokenRefreshLock) { |       synchronized (this.globalAccessTokenRefreshLock) { | ||||||
|         if (this.wxCpConfigStorage.isAccessTokenExpired()) { |         if (this.configStorage.isAccessTokenExpired()) { | ||||||
|           String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" |           String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | ||||||
|               + "&corpid=" + this.wxCpConfigStorage.getCorpId() + "&corpsecret=" |  + "&corpid=" + this.configStorage.getCorpId() | ||||||
|               + this.wxCpConfigStorage.getCorpSecret(); |               + "&corpsecret=" + this.configStorage.getCorpSecret(); | ||||||
|           try { |           try { | ||||||
|             HttpGet httpGet = new HttpGet(url); |             HttpGet httpGet = new HttpGet(url); | ||||||
|             if (this.httpProxy != null) { |             if (this.httpProxy != null) { | ||||||
| @ -129,7 +129,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|               throw new WxErrorException(error); |               throw new WxErrorException(error); | ||||||
|             } |             } | ||||||
|             WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); |             WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | ||||||
|             this.wxCpConfigStorage.updateAccessToken( |             this.configStorage.updateAccessToken( | ||||||
|                 accessToken.getAccessToken(), accessToken.getExpiresIn()); |                 accessToken.getAccessToken(), accessToken.getExpiresIn()); | ||||||
|           } catch (ClientProtocolException e) { |           } catch (ClientProtocolException e) { | ||||||
|             throw new RuntimeException(e); |             throw new RuntimeException(e); | ||||||
| @ -139,7 +139,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     return this.wxCpConfigStorage.getAccessToken(); |     return this.configStorage.getAccessToken(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -150,23 +150,23 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|   @Override |   @Override | ||||||
|   public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { |   public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { | ||||||
|     if (forceRefresh) { |     if (forceRefresh) { | ||||||
|       this.wxCpConfigStorage.expireJsapiTicket(); |       this.configStorage.expireJsapiTicket(); | ||||||
|     } |     } | ||||||
|     if (this.wxCpConfigStorage.isJsapiTicketExpired()) { |     if (this.configStorage.isJsapiTicketExpired()) { | ||||||
|       synchronized (this.globalJsapiTicketRefreshLock) { |       synchronized (this.globalJsapiTicketRefreshLock) { | ||||||
|         if (this.wxCpConfigStorage.isJsapiTicketExpired()) { |         if (this.configStorage.isJsapiTicketExpired()) { | ||||||
|           String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket"; |           String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket"; | ||||||
|           String responseContent = execute(new SimpleGetRequestExecutor(), url, null); |           String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | ||||||
|           JsonElement tmpJsonElement = new JsonParser().parse(responseContent); |           JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | ||||||
|           JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); |           JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | ||||||
|           String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); |           String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | ||||||
|           int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); |           int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); | ||||||
|           this.wxCpConfigStorage.updateJsapiTicket(jsapiTicket, |           this.configStorage.updateJsapiTicket(jsapiTicket, | ||||||
|               expiresInSeconds); |               expiresInSeconds); | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     return this.wxCpConfigStorage.getJsapiTicket(); |     return this.configStorage.getJsapiTicket(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -187,7 +187,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|     jsapiSignature.setSignature(signature); |     jsapiSignature.setSignature(signature); | ||||||
|      |      | ||||||
|     // Fixed bug |     // Fixed bug | ||||||
|     jsapiSignature.setAppid(this.wxCpConfigStorage.getCorpId()); |     jsapiSignature.setAppid(this.configStorage.getCorpId()); | ||||||
|      |      | ||||||
|     return jsapiSignature; |     return jsapiSignature; | ||||||
|   } |   } | ||||||
| @ -200,19 +200,19 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public void menuCreate(WxMenu menu) throws WxErrorException { |   public void menuCreate(WxMenu menu) throws WxErrorException { | ||||||
|     menuCreate(this.wxCpConfigStorage.getAgentId(), menu); |     menuCreate(this.configStorage.getAgentId(), menu); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public void menuCreate(String agentId, WxMenu menu) throws WxErrorException { |   public void menuCreate(String agentId, WxMenu menu) throws WxErrorException { | ||||||
|     String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" |     String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" | ||||||
|         + this.wxCpConfigStorage.getAgentId(); |  + this.configStorage.getAgentId(); | ||||||
|     post(url, menu.toJson()); |     post(url, menu.toJson()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public void menuDelete() throws WxErrorException { |   public void menuDelete() throws WxErrorException { | ||||||
|     menuDelete(this.wxCpConfigStorage.getAgentId()); |     menuDelete(this.configStorage.getAgentId()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -223,7 +223,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMenu menuGet() throws WxErrorException { |   public WxMenu menuGet() throws WxErrorException { | ||||||
|     return menuGet(this.wxCpConfigStorage.getAgentId()); |     return menuGet(this.configStorage.getAgentId()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -258,7 +258,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|     String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"; |     String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"; | ||||||
|     return execute( |     return execute( | ||||||
|         new MediaDownloadRequestExecutor( |         new MediaDownloadRequestExecutor( | ||||||
|             this.wxCpConfigStorage.getTmpDirFile()), | this.configStorage.getTmpDirFile()), | ||||||
|         url, "media_id=" + media_id); |         url, "media_id=" + media_id); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @ -475,7 +475,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|   @Override |   @Override | ||||||
| 	public String oauth2buildAuthorizationUrl(String state) { | 	public String oauth2buildAuthorizationUrl(String state) { | ||||||
|   	return this.oauth2buildAuthorizationUrl( |   	return this.oauth2buildAuthorizationUrl( | ||||||
|   		this.wxCpConfigStorage.getOauth2redirectUri(), | this.configStorage.getOauth2redirectUri(), | ||||||
|   		state |   		state | ||||||
|   	); |   	); | ||||||
| 	} | 	} | ||||||
| @ -483,7 +483,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
| 	@Override | 	@Override | ||||||
|   public String oauth2buildAuthorizationUrl(String redirectUri, String state) { |   public String oauth2buildAuthorizationUrl(String redirectUri, String state) { | ||||||
|     String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; |     String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; | ||||||
|     url += "appid=" + this.wxCpConfigStorage.getCorpId(); |     url += "appid=" + this.configStorage.getCorpId(); | ||||||
|     url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri); |     url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri); | ||||||
|     url += "&response_type=code"; |     url += "&response_type=code"; | ||||||
|     url += "&scope=snsapi_base"; |     url += "&scope=snsapi_base"; | ||||||
| @ -496,7 +496,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public String[] oauth2getUserInfo(String code) throws WxErrorException { |   public String[] oauth2getUserInfo(String code) throws WxErrorException { | ||||||
|     return oauth2getUserInfo(this.wxCpConfigStorage.getAgentId(), code); |     return oauth2getUserInfo(this.configStorage.getAgentId(), code); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -599,7 +599,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|        */ |        */ | ||||||
|       if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { |       if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { | ||||||
|         // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token |         // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token | ||||||
|         this.wxCpConfigStorage.expireAccessToken(); |         this.configStorage.expireAccessToken(); | ||||||
|         return execute(executor, uri, data); |         return execute(executor, uri, data); | ||||||
|       } |       } | ||||||
|       if (error.getErrorCode() != 0) { |       if (error.getErrorCode() != 0) { | ||||||
| @ -619,20 +619,22 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) { |   public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) { | ||||||
|     this.wxCpConfigStorage = wxConfigProvider; |     this.configStorage = wxConfigProvider; | ||||||
|     ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxCpConfigStorage |     ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage | ||||||
|         .getApacheHttpClientBuilder(); |         .getApacheHttpClientBuilder(); | ||||||
|     if (null == apacheHttpClientBuilder) { |     if (null == apacheHttpClientBuilder) { | ||||||
|       apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get(); |       apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get(); | ||||||
|     } |     } | ||||||
|     apacheHttpClientBuilder |  | ||||||
|         .httpProxyHost(this.wxCpConfigStorage.getHttpProxyHost()) |  | ||||||
|         .httpProxyPort(this.wxCpConfigStorage.getHttpProxyPort()) |  | ||||||
|         .httpProxyUsername(this.wxCpConfigStorage.getHttpProxyUsername()) |  | ||||||
|         .httpProxyPassword(this.wxCpConfigStorage.getHttpProxyPassword()); |  | ||||||
|  |  | ||||||
|     this.httpProxy = new HttpHost(this.wxCpConfigStorage.getHttpProxyHost(), |     apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost()) | ||||||
|         this.wxCpConfigStorage.getHttpProxyPort()); |         .httpProxyPort(this.configStorage.getHttpProxyPort()) | ||||||
|  |         .httpProxyUsername(this.configStorage.getHttpProxyUsername()) | ||||||
|  |         .httpProxyPassword(this.configStorage.getHttpProxyPassword()); | ||||||
|  |  | ||||||
|  |     if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) { | ||||||
|  |       this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     this.httpClient = apacheHttpClientBuilder.build(); |     this.httpClient = apacheHttpClientBuilder.build(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,12 +1,14 @@ | |||||||
| package me.chanjar.weixin.cp.api; | package me.chanjar.weixin.cp.api; | ||||||
|  |  | ||||||
| import com.google.inject.Inject; |  | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; |  | ||||||
| import me.chanjar.weixin.common.util.StringUtils; |  | ||||||
| import org.testng.Assert; | import org.testng.Assert; | ||||||
| import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||||
| import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||||
|  |  | ||||||
|  | import com.google.inject.Inject; | ||||||
|  |  | ||||||
|  | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
|  | import me.chanjar.weixin.common.util.StringUtils; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 基础API测试 |  * 基础API测试 | ||||||
|  * |  * | ||||||
| @ -20,7 +22,7 @@ public class WxCpBaseAPITest { | |||||||
|   protected WxCpServiceImpl wxService; |   protected WxCpServiceImpl wxService; | ||||||
|  |  | ||||||
|   public void testRefreshAccessToken() throws WxErrorException { |   public void testRefreshAccessToken() throws WxErrorException { | ||||||
|     WxCpConfigStorage configStorage = this.wxService.wxCpConfigStorage; |     WxCpConfigStorage configStorage = this.wxService.configStorage; | ||||||
|     String before = configStorage.getAccessToken(); |     String before = configStorage.getAccessToken(); | ||||||
|     this.wxService.getAccessToken(false); |     this.wxService.getAccessToken(false); | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,11 +1,13 @@ | |||||||
| package me.chanjar.weixin.cp.api; | package me.chanjar.weixin.cp.api; | ||||||
|  |  | ||||||
|  | import org.testng.annotations.Guice; | ||||||
|  | import org.testng.annotations.Test; | ||||||
|  |  | ||||||
| import com.google.inject.Inject; | import com.google.inject.Inject; | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.api.WxConsts; | import me.chanjar.weixin.common.api.WxConsts; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.cp.bean.WxCpMessage; | import me.chanjar.weixin.cp.bean.WxCpMessage; | ||||||
| import org.testng.annotations.Guice; |  | ||||||
| import org.testng.annotations.Test; |  | ||||||
|  |  | ||||||
| /*** | /*** | ||||||
|  * 测试发送消息 |  * 测试发送消息 | ||||||
| @ -20,7 +22,7 @@ public class WxCpMessageAPITest { | |||||||
|   protected WxCpServiceImpl wxService; |   protected WxCpServiceImpl wxService; | ||||||
|  |  | ||||||
|   public void testSendCustomMessage() throws WxErrorException { |   public void testSendCustomMessage() throws WxErrorException { | ||||||
|     ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.wxCpConfigStorage; |     ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.configStorage; | ||||||
|     WxCpMessage message1 = new WxCpMessage(); |     WxCpMessage message1 = new WxCpMessage(); | ||||||
|     message1.setAgentId(configStorage.getAgentId()); |     message1.setAgentId(configStorage.getAgentId()); | ||||||
|     message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT); |     message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT); | ||||||
|  | |||||||
| @ -515,8 +515,10 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|       apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); |       apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), |     if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) { | ||||||
|         this.configStorage.getHttpProxyPort()); |       this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     this.httpClient = apacheHttpClientBuilder.build(); |     this.httpClient = apacheHttpClientBuilder.build(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang