拦截器Interceptor 记录访问信息,通过aspect也可以,下面是通过Interceptor记录的
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT");
//url
LOGGER.info("url={}", request.getRequestURL());
//method
LOGGER.info("method={}", request.getMethod());
//ip
LOGGER.info("ip={}", request.getRemoteAddr());
if (handler instanceof HandlerMethod) {
HandlerMethod h = (HandlerMethod) handler;
//类方法
LOGGER.info("class_method={}", h.getBean().getClass().getName() + "." + h.getMethod().getName());
//参数
LOGGER.info("args={}", getParamString(request.getParameterMap()));
// StringBuilder sb = new StringBuilder(1000);
//
// sb.append("Controller: ").append(h.getBean().getClass().getName()).append("\n");
// sb.append("Method : ").append(h.getMethod().getName()).append("\n");
// sb.append("Params : ").append(request.getParameterMap()).append("\n");
// sb.append("URI : ").append(request.getRequestURI()).append("\n");
// System.out.println(sb.toString());
}
return !request.getMethod().equals("OPTIONS");
}
private String getParamString(Map<String, String[]> map) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String[]> e : map.entrySet()) {
sb.append(e.getKey()).append(":");
String[] value = e.getValue();
if (value != null && value.length == 1) {
sb.append(value[0]).append("\t");
} else {
sb.append(Arrays.toString(value)).append("\t");
}
}
return sb.toString();
}