Skip to content

Commit 75069ba

Browse files
committed
binarywang#556 日志信息中如果含有secret值的,将其值隐藏掉
1 parent 329847e commit 75069ba

File tree

6 files changed

+78
-18
lines changed

6 files changed

+78
-18
lines changed

weixin-java-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@
109109
<artifactId>jetty-servlet</artifactId>
110110
<scope>test</scope>
111111
</dependency>
112+
<dependency>
113+
<groupId>org.assertj</groupId>
114+
<artifactId>assertj-guava</artifactId>
115+
<scope>test</scope>
116+
</dependency>
112117
</dependencies>
113118

114119
<build>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package me.chanjar.weixin.common.util;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
/**
6+
* <pre>
7+
* 数据处理工具类
8+
* Created by BinaryWang on 2018/5/8.
9+
* </pre>
10+
*
11+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
12+
*/
13+
public class DataUtils {
14+
/**
15+
* 将数据中包含的secret字符使用星号替换,防止日志打印时被输出
16+
*/
17+
public static <E> E handleDataWithSecret(E data) {
18+
E dataForLog = data;
19+
if(data instanceof String && StringUtils.contains((String)data, "&secret=")){
20+
dataForLog = (E) StringUtils.replaceAll((String)data,"&secret=\\w+&","&secret=******&");
21+
}
22+
return dataForLog;
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.chanjar.weixin.common.util;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.testng.Assert.*;
7+
8+
/**
9+
* <pre>
10+
* Created by BinaryWang on 2018/5/8.
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
*/
15+
public class DataUtilsTest {
16+
17+
@Test
18+
public void testHandleDataWithSecret() {
19+
String data = "js_code=001tZveq0SMoiq1AEXeq0ECJeq0tZveZ&secret=5681022fa1643845392367ea88888888&grant_type=authorization_code&appid=wxe156d4848d999999";
20+
final String s = DataUtils.handleDataWithSecret(data);
21+
assertThat(s).contains("&secret=******&");
22+
}
23+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import com.google.gson.JsonObject;
66
import com.google.gson.JsonParser;
77
import me.chanjar.weixin.common.bean.WxJsapiSignature;
8-
import me.chanjar.weixin.common.bean.menu.WxMenu;
98
import me.chanjar.weixin.common.bean.result.WxError;
10-
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
119
import me.chanjar.weixin.common.exception.WxErrorException;
1210
import me.chanjar.weixin.common.session.StandardSessionManager;
1311
import me.chanjar.weixin.common.session.WxSession;
1412
import me.chanjar.weixin.common.session.WxSessionManager;
13+
import me.chanjar.weixin.common.util.DataUtils;
1514
import me.chanjar.weixin.common.util.RandomUtils;
1615
import me.chanjar.weixin.common.util.crypto.SHA1;
1716
import me.chanjar.weixin.common.util.http.RequestExecutor;
@@ -21,14 +20,11 @@
2120
import me.chanjar.weixin.cp.api.*;
2221
import me.chanjar.weixin.cp.bean.*;
2322
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
24-
import org.apache.commons.lang3.StringUtils;
2523
import org.slf4j.Logger;
2624
import org.slf4j.LoggerFactory;
2725

2826
import java.io.File;
2927
import java.io.IOException;
30-
import java.io.InputStream;
31-
import java.util.List;
3228

3329
public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, RequestHttp<H, P> {
3430
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@@ -201,6 +197,8 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
201197
}
202198

203199
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
200+
E dataForLog = DataUtils.handleDataWithSecret(data);
201+
204202
if (uri.contains("access_token=")) {
205203
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
206204
}
@@ -210,7 +208,7 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
210208

211209
try {
212210
T result = executor.execute(uriWithAccessToken, data);
213-
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, data, result);
211+
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
214212
return result;
215213
} catch (WxErrorException e) {
216214
WxError error = e.getError();
@@ -227,12 +225,12 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
227225
}
228226

229227
if (error.getErrorCode() != 0) {
230-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, data, error);
228+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error);
231229
throw new WxErrorException(error, e);
232230
}
233231
return null;
234232
} catch (IOException e) {
235-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, data, e.getMessage());
233+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage());
236234
throw new RuntimeException(e);
237235
}
238236
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import me.chanjar.weixin.common.bean.WxAccessToken;
1717
import me.chanjar.weixin.common.bean.result.WxError;
1818
import me.chanjar.weixin.common.exception.WxErrorException;
19+
import me.chanjar.weixin.common.util.DataUtils;
1920
import me.chanjar.weixin.common.util.crypto.SHA1;
2021
import me.chanjar.weixin.common.util.http.HttpType;
2122
import me.chanjar.weixin.common.util.http.RequestExecutor;
@@ -24,6 +25,7 @@
2425
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
2526
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
2627
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
28+
import org.apache.commons.lang3.StringUtils;
2729
import org.apache.http.HttpHost;
2830
import org.apache.http.client.config.RequestConfig;
2931
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -38,6 +40,9 @@
3840
import java.util.Map;
3941
import java.util.concurrent.locks.Lock;
4042

43+
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.*;
44+
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*;
45+
4146
/**
4247
* @author <a href="https://github.com/binarywang">Binary Wang</a>
4348
*/
@@ -212,7 +217,9 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
212217
throw new RuntimeException("微信服务端异常,超出重试次数");
213218
}
214219

215-
public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
220+
private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
221+
E dataForLog = DataUtils.handleDataWithSecret(data);
222+
216223
if (uri.contains("access_token=")) {
217224
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
218225
}
@@ -222,16 +229,16 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
222229

223230
try {
224231
T result = executor.execute(uriWithAccessToken, data);
225-
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, data, result);
232+
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
226233
return result;
227234
} catch (WxErrorException e) {
228235
WxError error = e.getError();
229236
/*
230237
* 发生以下情况时尝试刷新access_token
231238
*/
232-
if (error.getErrorCode() == WxMaConstants.ErrorCode.ERR_40001
233-
|| error.getErrorCode() == WxMaConstants.ErrorCode.ERR_42001
234-
|| error.getErrorCode() == WxMaConstants.ErrorCode.ERR_40014) {
239+
if (error.getErrorCode() == ERR_40001
240+
|| error.getErrorCode() == ERR_42001
241+
|| error.getErrorCode() == ERR_40014) {
235242
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
236243
this.getWxMaConfig().expireAccessToken();
237244
if (this.getWxMaConfig().autoRefreshToken()) {
@@ -240,12 +247,12 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
240247
}
241248

242249
if (error.getErrorCode() != 0) {
243-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, data, error);
250+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error);
244251
throw new WxErrorException(error, e);
245252
}
246253
return null;
247254
} catch (IOException e) {
248-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, data, e.getMessage());
255+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage());
249256
throw new RuntimeException(e);
250257
}
251258
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import me.chanjar.weixin.common.exception.WxErrorException;
1010
import me.chanjar.weixin.common.session.StandardSessionManager;
1111
import me.chanjar.weixin.common.session.WxSessionManager;
12+
import me.chanjar.weixin.common.util.DataUtils;
1213
import me.chanjar.weixin.common.util.RandomUtils;
1314
import me.chanjar.weixin.common.util.crypto.SHA1;
1415
import me.chanjar.weixin.common.util.http.*;
@@ -265,6 +266,8 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
265266
}
266267

267268
public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
269+
E dataForLog = DataUtils.handleDataWithSecret(data);
270+
268271
if (uri.contains("access_token=")) {
269272
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
270273
}
@@ -275,7 +278,7 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
275278

276279
try {
277280
T result = executor.execute(uriWithAccessToken, data);
278-
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, data, result);
281+
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
279282
return result;
280283
} catch (WxErrorException e) {
281284
WxError error = e.getError();
@@ -294,12 +297,12 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
294297
}
295298

296299
if (error.getErrorCode() != 0) {
297-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, data, error);
300+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error);
298301
throw new WxErrorException(error, e);
299302
}
300303
return null;
301304
} catch (IOException e) {
302-
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, data, e.getMessage());
305+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage());
303306
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
304307
}
305308
}

0 commit comments

Comments
 (0)