Commit 9d834a9b authored by Java-刘 彧阳's avatar Java-刘 彧阳

往外吐手机号的日志记录

parent 65324010
package cn.quantgroup.xyqb.aspect.logcaller;
import cn.quantgroup.xyqb.util.ApplicationContextHolder;
import cn.quantgroup.xyqb.util.IPUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* 调用者记录
* Created by Administrator on 2017/5/15.
*/
@Aspect
@Component
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class LogCallHttpAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(LogCallHttpAspect.class);
@Pointcut("@annotation(cn.quantgroup.xyqb.aspect.logcaller.LogHttpCaller)")
private void logHttpCaller() {
}
@Around("logHttpCaller()")
public Object record(ProceedingJoinPoint pjp) throws Throwable {
Object result = pjp.proceed();
//todo 异步记录调用日志
try {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attrs.getRequest();
String remoteIP = IPUtil.getRemoteIP(request);
LogCallHttpAspect logCallHttpAspect = ApplicationContextHolder.getBean(LogCallHttpAspect.class);
logCallHttpAspect.asyncRecordIt(pjp,result,remoteIP);
}catch (Exception e){
}
return result;
}
@Async
public void asyncRecordIt(ProceedingJoinPoint pjp, Object result, String remoteIP){
Object[] args = pjp.getArgs();
String methodName = pjp.getSignature().getName();
String targetName = pjp.getTarget().getClass().getName();
LOGGER.info("http api is called,from IP:[{}],method :[{}],args:[{}],response:[{}]",remoteIP,targetName.concat(".").concat(methodName),args,result);
}
}
package cn.quantgroup.xyqb.aspect.logcaller;
import java.lang.annotation.*;
/**
* Created by Administrator on 2017/5/15.
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogHttpCaller {
}
package cn.quantgroup.xyqb.controller.external.user; package cn.quantgroup.xyqb.controller.external.user;
import cn.quantgroup.xyqb.aspect.logcaller.LogHttpCaller;
import cn.quantgroup.xyqb.controller.IBaseController; import cn.quantgroup.xyqb.controller.IBaseController;
import cn.quantgroup.xyqb.entity.*; import cn.quantgroup.xyqb.entity.*;
import cn.quantgroup.xyqb.entity.enumerate.*; import cn.quantgroup.xyqb.entity.enumerate.*;
...@@ -56,6 +57,7 @@ public class InnerController implements IBaseController { ...@@ -56,6 +57,7 @@ public class InnerController implements IBaseController {
private ISessionService sessionService; private ISessionService sessionService;
@RequestMapping("/user/search/phoneNo") @RequestMapping("/user/search/phoneNo")
@LogHttpCaller
public JsonResult findByPhoneNo(String phoneNo) { public JsonResult findByPhoneNo(String phoneNo) {
User user = userService.findByPhoneInDb(phoneNo); User user = userService.findByPhoneInDb(phoneNo);
if (user == null) { if (user == null) {
...@@ -66,6 +68,7 @@ public class InnerController implements IBaseController { ...@@ -66,6 +68,7 @@ public class InnerController implements IBaseController {
} }
@RequestMapping("/user/search/uuid") @RequestMapping("/user/search/uuid")
@LogHttpCaller
public JsonResult findByUuid(String uuid) { public JsonResult findByUuid(String uuid) {
User user = userService.findByUuidInDb(uuid); User user = userService.findByUuidInDb(uuid);
if (user == null) { if (user == null) {
...@@ -76,6 +79,7 @@ public class InnerController implements IBaseController { ...@@ -76,6 +79,7 @@ public class InnerController implements IBaseController {
} }
@RequestMapping("/user/getPhoneByUserIds") @RequestMapping("/user/getPhoneByUserIds")
@LogHttpCaller
public JsonResult findByIds(@RequestParam(value = "userIds") String userIdsString) { public JsonResult findByIds(@RequestParam(value = "userIds") String userIdsString) {
LOGGER.info("批量查询用户的手机号列表, userIdsString:" + userIdsString); LOGGER.info("批量查询用户的手机号列表, userIdsString:" + userIdsString);
if (StringUtils.isEmpty(userIdsString)) { if (StringUtils.isEmpty(userIdsString)) {
......
package cn.quantgroup.xyqb.util; package cn.quantgroup.xyqb.util;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @author mengfan.feng * @author mengfan.feng
*/ */
public class Utils { public class Utils {
private static Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[34578]\\d{9})|(?:861[34578]\\d{9}))(?!\\d)");
public static String safeMap2Str(Map<String, String> info) { public static String safeMap2Str(Map<String, String> info) {
if (null == info) { if (null == info) {
return null; return null;
...@@ -26,4 +32,22 @@ public class Utils { ...@@ -26,4 +32,22 @@ public class Utils {
return builder.toString(); return builder.toString();
} }
public static String coverPhone(String str){
if(str.length()<=0)
return "";
if(str.length()>1024) //长度超过1024的字符串不走正则匹配过滤手机号
return "";
Matcher matcher = pattern.matcher(str);
Map<String,String> replaceMap = new HashMap<>();
while (matcher.find()) {
String group = matcher.group();
String replace = group.substring(0,3).concat("****").concat(group.substring(7)).concat("[".concat(PasswordUtil.MD5(group)).concat("]"));
replaceMap.put(group,replace);
}
Set<Map.Entry<String, String>> entries = replaceMap.entrySet();
for (Map.Entry<String, String> entry :entries){
str = str.replaceAll(entry.getKey(),entry.getValue());
}
return str;
}
} }
...@@ -9,6 +9,10 @@ import java.util.Map; ...@@ -9,6 +9,10 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.Utils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.slf4j.Marker; import org.slf4j.Marker;
/** /**
...@@ -16,8 +20,6 @@ import org.slf4j.Marker; ...@@ -16,8 +20,6 @@ import org.slf4j.Marker;
*/ */
public class WithOutPhoneLoggingEvent implements ILoggingEvent { public class WithOutPhoneLoggingEvent implements ILoggingEvent {
private static Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[34578]\\d{9})|(?:861[34578]\\d{9}))(?!\\d)");
private ILoggingEvent event; private ILoggingEvent event;
transient String withOutPhoneFormattedMessage; transient String withOutPhoneFormattedMessage;
...@@ -46,9 +48,8 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent { ...@@ -46,9 +48,8 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent {
if (withOutPhoneFormattedMessage != null) { if (withOutPhoneFormattedMessage != null) {
return withOutPhoneFormattedMessage; return withOutPhoneFormattedMessage;
} }
//todo 用正则找到手机号,并打上掩码,替换进去 // 用正则找到手机号,并打上掩码,替换进去
//withOutPhoneFormattedMessage = event.getFormattedMessage(); withOutPhoneFormattedMessage = Utils.coverPhone(event.getFormattedMessage());
withOutPhoneFormattedMessage = coverPhone(event.getFormattedMessage());
return withOutPhoneFormattedMessage; return withOutPhoneFormattedMessage;
} }
...@@ -95,20 +96,4 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent { ...@@ -95,20 +96,4 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent {
this.getMDCPropertyMap(); this.getMDCPropertyMap();
} }
private static String coverPhone(String str){
if(str.length()<=0)
return "";
Matcher matcher = pattern.matcher(str);
Map<String,String> replaceMap = new HashMap<>();
while (matcher.find()) {
String group = matcher.group();
String replace = group.substring(0,3).concat("****").concat(group.substring(7));
replaceMap.put(group,replace);
}
Set<Map.Entry<String, String>> entries = replaceMap.entrySet();
for (Map.Entry<String, String> entry :entries){
str = str.replaceAll(entry.getKey(),entry.getValue());
}
return str;
}
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
--> -->
<layout class="ch.qos.logback.classic.PatternLayout"> <layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{ISO8601} [%-5level] %logger{36} - %msg%n</pattern> <pattern>%d{ISO8601} [%thread] [%-5level] %logger{36} - %msg%n</pattern>
</layout> </layout>
</appender> </appender>
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<maxHistory>30</maxHistory> <maxHistory>30</maxHistory>
</rollingPolicy> </rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout"> <layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{ISO8601} [%-5level] %logger{36} - %msg%n</pattern> <pattern>%d{ISO8601} [%thread] [%-5level] %logger{36} - %msg%n</pattern>
</layout> </layout>
</appender> </appender>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment