1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
@WebFilter( "/") @Component public class CommonFilter implements Filter {
public String uuid;
private final Log log = LogFactory.getLog(CommonFilter.class);
private final String[] filterList = { "/doc.html.*", "/webjars.*", "/swagger-resources.*", "/v2.*", "/swagger.*" };
@Override public void init(FilterConfig filterConfig) throws ServletException {
}
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response;
String url = req.getRequestURL().toString(); String uri = req.getRequestURI(); String clientIp=req.getRemoteAddr(); Integer clientPort=req.getRemotePort(); String serverName=req.getRemoteHost(); String ua = req.getHeader("User-Agent");
String uuid = UUID.randomUUID().toString().substring(0,8); this.uuid = uuid; boolean isMatch = true; for (String pattern : filterList){ if (Pattern.matches(pattern, uri)){ isMatch = false; break; } } if (isMatch == true){ ObjectMapper objectMapper = new ObjectMapper(); Map<String,String> map = objectMapper.readValue(CommonUtils.httpGet("https://www.fkcoder.com/ip?ip="+clientIp,"GET"),Map.class); String address = map.get("country") + "," + map.get("province") + "," + map.get("city") + "," + map.get("isp");
req.setAttribute("uuid",uuid); req.setAttribute("startTime",System.currentTimeMillis());
log.info(String.format("[-][%s] === 请求开始",uuid)); log.info(String.format("[-][%s] url: %s",uuid,url)); log.info(String.format("[-][%s] 客户端信息: ip 地址:%s (%s),端口号:%s,计算机名称:%s",uuid,clientIp,address,clientPort,serverName)); log.info(String.format("[-][%s] User-Agent:%s",uuid,ua)); chain.doFilter(request, response);
long startTime = (long) req.getAttribute("startTime"); long stopTime = System.currentTimeMillis(); String uuid1 = (String) req.getAttribute("uuid");
log.info(String.format("[-][%s] === 请求完成,请求执行时间 %.4f 秒",uuid1,(stopTime-startTime)/1000.0)); }else { chain.doFilter(request, response); } }
@Override public void destroy() {
} }
|