#556 日志信息中如果含有secret值的,将其值隐藏掉

This commit is contained in:
Binary Wang
2018-05-08 23:19:03 +08:00
parent 329847eb90
commit 75069baad4
6 changed files with 78 additions and 18 deletions

View File

@@ -109,6 +109,11 @@
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,24 @@
package me.chanjar.weixin.common.util;
import org.apache.commons.lang3.StringUtils;
/**
* <pre>
* 数据处理工具类
* Created by BinaryWang on 2018/5/8.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class DataUtils {
/**
* 将数据中包含的secret字符使用星号替换防止日志打印时被输出
*/
public static <E> E handleDataWithSecret(E data) {
E dataForLog = data;
if(data instanceof String && StringUtils.contains((String)data, "&secret=")){
dataForLog = (E) StringUtils.replaceAll((String)data,"&secret=\\w+&","&secret=******&");
}
return dataForLog;
}
}

View File

@@ -0,0 +1,23 @@
package me.chanjar.weixin.common.util;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.*;
/**
* <pre>
* Created by BinaryWang on 2018/5/8.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class DataUtilsTest {
@Test
public void testHandleDataWithSecret() {
String data = "js_code=001tZveq0SMoiq1AEXeq0ECJeq0tZveZ&secret=5681022fa1643845392367ea88888888&grant_type=authorization_code&appid=wxe156d4848d999999";
final String s = DataUtils.handleDataWithSecret(data);
assertThat(s).contains("&secret=******&");
}
}