@Order(5)
@Aspect
@Component
public class AnnotationInterceptor {
private static Logger logger = LogManager.getLogger(AnnotationInterceptor.class.getName());
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping) || "
+ "@annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.PutMapping) || @annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void RequestMapping() {
}
@Around("RequestMapping()")
public Object Interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
// String token =request.getHeader(ConstantUtil.X_VANRUI_TOKEN);
// if (StringUtils.isEmpty(token)) {// 不携带token
// return new ResponseDTO(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), "没有token");
// }
//
// SymmetricEncoderutil se = new SymmetricEncoderutil();
// try {
// String token2 = se.AESDncode("vanrui",token);
// String[] tokens = token2.split("_");
// request.setAttribute("companyId", tokens[1]);
// } catch (Exception e) {
// return new ResponseDTO(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), "token无效 请重新登录");
// }
// IP地址
String ipAddr = getRemoteHost(request);
String url = request.getRequestURL().toString();
String reqParam = reqParam(request);
if (!url.contains("api/appToken/validate")) {
logger.info("[{}],[{}],[{}]", ipAddr, url, reqParam);
}
Object result = joinPoint.proceed();
return result;
}
/**
* 入参数据
*
* @param request
* @return
*/
private String reqParam(HttpServletRequest request) {
return JSON.toJSONString(request.getParameterMap());
}
/**
* 获取目标主机的ip
*
* @param request
* @return
*/
private String getRemoteHost(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
}
springboot请求url日志记录
最新推荐文章于 2024-10-10 16:06:37 发布
该博客介绍了如何使用Spring AOP创建一个注解拦截器,记录带有@RequestMapping、@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注解的请求URL日志。拦截器不仅获取并记录了IP地址和请求参数,还包含了一个未实现的功能,即检查请求头中的token有效性。
1228

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



