Skip to content

Commit 1d8326e

Browse files
committed
修复公众号proxy为空的bug,并发布临时版本2.1.2
1 parent 023ed9a commit 1d8326e

File tree

5 files changed

+86
-50
lines changed

5 files changed

+86
-50
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.github.binarywang</groupId>
77
<artifactId>weixin-java-parent</artifactId>
8-
<version>2.1.1</version>
8+
<version>2.1.2</version>
99
<packaging>pom</packaging>
1010
<name>WeiXin Java Tools - Parent</name>
1111
<description>微信公众号、企业号上级POM</description>

weixin-java-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.binarywang</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>2.1.1</version>
9+
<version>2.1.2</version>
1010
</parent>
1111

1212
<artifactId>weixin-java-common</artifactId>

weixin-java-cp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.binarywang</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>2.1.1</version>
9+
<version>2.1.2</version>
1010
</parent>
1111

1212
<artifactId>weixin-java-cp</artifactId>

weixin-java-mp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.binarywang</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>2.1.1</version>
9+
<version>2.1.2</version>
1010
</parent>
1111
<artifactId>weixin-java-mp</artifactId>
1212
<name>WeiXin Java Tools - MP</name>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.io.IOException;
4+
5+
import org.apache.http.HttpHost;
6+
import org.apache.http.client.config.RequestConfig;
7+
import org.apache.http.client.methods.CloseableHttpResponse;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
10+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
11+
import org.apache.http.impl.client.BasicResponseHandler;
12+
import org.apache.http.impl.client.CloseableHttpClient;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
316
import com.google.gson.JsonArray;
417
import com.google.gson.JsonElement;
518
import com.google.gson.JsonObject;
619
import com.google.gson.JsonParser;
20+
721
import me.chanjar.weixin.common.bean.WxAccessToken;
822
import me.chanjar.weixin.common.bean.WxJsapiSignature;
923
import me.chanjar.weixin.common.bean.result.WxError;
@@ -12,22 +26,38 @@
1226
import me.chanjar.weixin.common.session.WxSessionManager;
1327
import me.chanjar.weixin.common.util.RandomUtils;
1428
import me.chanjar.weixin.common.util.crypto.SHA1;
15-
import me.chanjar.weixin.common.util.http.*;
16-
import me.chanjar.weixin.mp.api.*;
17-
import me.chanjar.weixin.mp.bean.*;
18-
import me.chanjar.weixin.mp.bean.result.*;
19-
import org.apache.http.HttpHost;
20-
import org.apache.http.client.config.RequestConfig;
21-
import org.apache.http.client.methods.CloseableHttpResponse;
22-
import org.apache.http.client.methods.HttpGet;
23-
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
24-
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
25-
import org.apache.http.impl.client.BasicResponseHandler;
26-
import org.apache.http.impl.client.CloseableHttpClient;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
29-
30-
import java.io.IOException;
29+
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
30+
import me.chanjar.weixin.common.util.http.DefaultApacheHttpClientBuilder;
31+
import me.chanjar.weixin.common.util.http.RequestExecutor;
32+
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
33+
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
34+
import me.chanjar.weixin.common.util.http.URIUtil;
35+
import me.chanjar.weixin.mp.api.WxMpCardService;
36+
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
37+
import me.chanjar.weixin.mp.api.WxMpDataCubeService;
38+
import me.chanjar.weixin.mp.api.WxMpGroupService;
39+
import me.chanjar.weixin.mp.api.WxMpKefuService;
40+
import me.chanjar.weixin.mp.api.WxMpMaterialService;
41+
import me.chanjar.weixin.mp.api.WxMpMenuService;
42+
import me.chanjar.weixin.mp.api.WxMpPayService;
43+
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
44+
import me.chanjar.weixin.mp.api.WxMpService;
45+
import me.chanjar.weixin.mp.api.WxMpUserService;
46+
import me.chanjar.weixin.mp.api.WxMpUserTagService;
47+
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
48+
import me.chanjar.weixin.mp.bean.WxMpIndustry;
49+
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
50+
import me.chanjar.weixin.mp.bean.WxMpMassNews;
51+
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
52+
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
53+
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
54+
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
55+
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
56+
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
57+
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
58+
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
59+
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
60+
import me.chanjar.weixin.mp.bean.result.WxMpUser;
3161

3262
public class WxMpServiceImpl implements WxMpService {
3363

@@ -45,7 +75,7 @@ public class WxMpServiceImpl implements WxMpService {
4575
*/
4676
private final Object globalJsapiTicketRefreshLock = new Object();
4777

48-
private WxMpConfigStorage wxMpConfigStorage;
78+
private WxMpConfigStorage configStorage;
4979

5080
private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this);
5181

@@ -80,7 +110,8 @@ public class WxMpServiceImpl implements WxMpService {
80110
@Override
81111
public boolean checkSignature(String timestamp, String nonce, String signature) {
82112
try {
83-
return SHA1.gen(this.wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature);
113+
return SHA1.gen(this.configStorage.getToken(), timestamp, nonce)
114+
.equals(signature);
84115
} catch (Exception e) {
85116
return false;
86117
}
@@ -94,14 +125,14 @@ public String getAccessToken() throws WxErrorException {
94125
@Override
95126
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
96127
if (forceRefresh) {
97-
this.wxMpConfigStorage.expireAccessToken();
128+
this.configStorage.expireAccessToken();
98129
}
99-
if (this.wxMpConfigStorage.isAccessTokenExpired()) {
130+
if (this.configStorage.isAccessTokenExpired()) {
100131
synchronized (this.globalAccessTokenRefreshLock) {
101-
if (this.wxMpConfigStorage.isAccessTokenExpired()) {
132+
if (this.configStorage.isAccessTokenExpired()) {
102133
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
103-
"&appid=" + this.wxMpConfigStorage.getAppId() +
104-
"&secret=" + this.wxMpConfigStorage.getSecret();
134+
"&appid=" + this.configStorage.getAppId() + "&secret="
135+
+ this.configStorage.getSecret();
105136
try {
106137
HttpGet httpGet = new HttpGet(url);
107138
if (this.httpProxy != null) {
@@ -115,7 +146,8 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
115146
throw new WxErrorException(error);
116147
}
117148
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
118-
this.wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
149+
this.configStorage.updateAccessToken(accessToken.getAccessToken(),
150+
accessToken.getExpiresIn());
119151
}finally {
120152
httpGet.releaseConnection();
121153
}
@@ -125,7 +157,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
125157
}
126158
}
127159
}
128-
return this.wxMpConfigStorage.getAccessToken();
160+
return this.configStorage.getAccessToken();
129161
}
130162

131163
@Override
@@ -136,23 +168,23 @@ public String getJsapiTicket() throws WxErrorException {
136168
@Override
137169
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
138170
if (forceRefresh) {
139-
this.wxMpConfigStorage.expireJsapiTicket();
171+
this.configStorage.expireJsapiTicket();
140172
}
141173

142-
if (this.wxMpConfigStorage.isJsapiTicketExpired()) {
174+
if (this.configStorage.isJsapiTicketExpired()) {
143175
synchronized (this.globalJsapiTicketRefreshLock) {
144-
if (this.wxMpConfigStorage.isJsapiTicketExpired()) {
176+
if (this.configStorage.isJsapiTicketExpired()) {
145177
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
146178
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
147179
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
148180
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
149181
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
150182
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
151-
this.wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
183+
this.configStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
152184
}
153185
}
154186
}
155-
return this.wxMpConfigStorage.getJsapiTicket();
187+
return this.configStorage.getJsapiTicket();
156188
}
157189

158190
@Override
@@ -163,7 +195,7 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
163195
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
164196
"noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
165197
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
166-
jsapiSignature.setAppid(this.wxMpConfigStorage.getAppId());
198+
jsapiSignature.setAppid(this.configStorage.getAppId());
167199
jsapiSignature.setTimestamp(timestamp);
168200
jsapiSignature.setNoncestr(noncestr);
169201
jsapiSignature.setUrl(url);
@@ -264,7 +296,7 @@ public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) th
264296
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
265297
StringBuilder url = new StringBuilder();
266298
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
267-
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
299+
url.append("appid=").append(this.configStorage.getAppId());
268300
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
269301
url.append("&response_type=code");
270302
url.append("&scope=").append(scope);
@@ -280,7 +312,7 @@ public String buildQrConnectUrl(String redirectURI, String scope,
280312
String state) {
281313
StringBuilder url = new StringBuilder();
282314
url.append("https://open.weixin.qq.com/connect/qrconnect?");
283-
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
315+
url.append("appid=").append(this.configStorage.getAppId());
284316
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
285317
url.append("&response_type=code");
286318
url.append("&scope=").append(scope);
@@ -306,8 +338,8 @@ private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxE
306338
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
307339
StringBuilder url = new StringBuilder();
308340
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
309-
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
310-
url.append("&secret=").append(this.wxMpConfigStorage.getSecret());
341+
url.append("appid=").append(this.configStorage.getAppId());
342+
url.append("&secret=").append(this.configStorage.getSecret());
311343
url.append("&code=").append(code);
312344
url.append("&grant_type=authorization_code");
313345

@@ -318,7 +350,7 @@ public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorExc
318350
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
319351
StringBuilder url = new StringBuilder();
320352
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
321-
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
353+
url.append("appid=").append(this.configStorage.getAppId());
322354
url.append("&grant_type=refresh_token");
323355
url.append("&refresh_token=").append(refreshToken);
324356

@@ -438,7 +470,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
438470
*/
439471
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
440472
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
441-
this.wxMpConfigStorage.expireAccessToken();
473+
this.configStorage.expireAccessToken();
442474
return execute(executor, uri, data);
443475
}
444476
if (error.getErrorCode() != 0) {
@@ -460,33 +492,37 @@ public CloseableHttpClient getHttpclient() {
460492

461493
@Override
462494
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
463-
this.wxMpConfigStorage = wxConfigProvider;
495+
this.configStorage = wxConfigProvider;
464496
this.initHttpClient();
465497
}
466498

467499
private void initHttpClient() {
468-
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder();
500+
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
501+
.getApacheHttpClientBuilder();
469502
if (null == apacheHttpClientBuilder) {
470503
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
471504
}
472505

473-
apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttpProxyHost())
474-
.httpProxyPort(this.wxMpConfigStorage.getHttpProxyPort())
475-
.httpProxyUsername(this.wxMpConfigStorage.getHttpProxyUsername())
476-
.httpProxyPassword(this.wxMpConfigStorage.getHttpProxyPassword());
506+
apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
507+
.httpProxyPort(this.configStorage.getHttpProxyPort())
508+
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
509+
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
477510

478-
if (this.wxMpConfigStorage.getSSLContext() != null){
511+
if (this.configStorage.getSSLContext() != null) {
479512
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
480-
this.wxMpConfigStorage.getSSLContext(), new String[] { "TLSv1" }, null, new DefaultHostnameVerifier());
513+
this.configStorage.getSSLContext(), new String[] { "TLSv1" }, null,
514+
new DefaultHostnameVerifier());
481515
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
482516
}
483517

518+
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(),
519+
this.configStorage.getHttpProxyPort());
484520
this.httpClient = apacheHttpClientBuilder.build();
485521
}
486522

487523
@Override
488524
public WxMpConfigStorage getWxMpConfigStorage() {
489-
return this.wxMpConfigStorage;
525+
return this.configStorage;
490526
}
491527

492528
@Override

0 commit comments

Comments
 (0)