项目里定义了一个切片来实现打印所有的controller请求日志,包括地址和参数,精简后大致如下:
@Before("log()")
public void beforeLog(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
log.info("START--BODY{}", JSONObject.toJSONString(args, SerializerFeature.WriteMapNullValue));
}
一般来讲正常的请求都不会有问题,但是如果遇到在controller中需要使用HttpServletResponse或HttpServletRequest时就可能会有问题,因为像OutPutStream之类的流只可以使用一次,自然在JSONObject.toJSONString中就会导致错误,这意味着在正式逻辑和在切片中一共使用了两次。
所以修改为:
@Before("log()")
public void beforeLog(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
args = Arrays.stream(args).filter(p -> !(p instanceof HttpServletResponse || p instanceof HttpServletRequest)).toArray();
log.info("START--BODY{}", JSONObject.toJSONString(args, SerializerFeature.WriteMapNullValue));
}
在序列化之前,将HttpServletRequest和HttpServletResponse的实现类对象剔除掉就可以了
本文介绍了一种在记录Controller请求日志时过滤掉HttpServletResponse和HttpServletRequest等敏感对象的方法,以避免因流对象被多次使用而导致的问题。
20万+

被折叠的 条评论
为什么被折叠?



