Commit b24098eb06a9ad5d0c779c883f4132cf0533333b
1 parent
9946c0da
1.加入访问日志信息
Showing
2 changed files
with
87 additions
and
0 deletions
src/main/java/com/bsth/filter/AccessFilter.java
0 → 100644
| 1 | +package com.bsth.filter; | |
| 2 | + | |
| 3 | +import com.bsth.util.IpUtils; | |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | +import org.slf4j.Logger; | |
| 7 | +import org.slf4j.LoggerFactory; | |
| 8 | +import org.springframework.stereotype.Component; | |
| 9 | + | |
| 10 | +import javax.servlet.FilterChain; | |
| 11 | +import javax.servlet.ServletException; | |
| 12 | +import javax.servlet.http.HttpServletRequest; | |
| 13 | +import javax.servlet.http.HttpServletResponse; | |
| 14 | +import java.io.IOException; | |
| 15 | +import java.util.Map; | |
| 16 | + | |
| 17 | +@Component | |
| 18 | +public class AccessFilter extends BaseFilter{ | |
| 19 | + | |
| 20 | + private Logger logger = LoggerFactory.getLogger(AccessFilter.class); | |
| 21 | + | |
| 22 | + private ObjectMapper mapper = new ObjectMapper(); | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) | |
| 26 | + throws IOException, ServletException { | |
| 27 | + | |
| 28 | + long start = System.currentTimeMillis(); | |
| 29 | + String sessionId = request.getRequestedSessionId(); | |
| 30 | + String ip = IpUtils.getIpAddr(request); | |
| 31 | + String userAgent = request.getHeader("User-Agent"); | |
| 32 | + String url = request.getRequestURI(); | |
| 33 | + | |
| 34 | + StringBuilder s = new StringBuilder(); | |
| 35 | + s.append(getBlock(sessionId)); | |
| 36 | + s.append(getBlock(ip)); | |
| 37 | + s.append(getBlock(userAgent)); | |
| 38 | + s.append(getBlock(url)); | |
| 39 | + s.append(getBlock(getParams(request))); | |
| 40 | + logger.info(s.toString()); | |
| 41 | + | |
| 42 | + chain.doFilter(request, response); | |
| 43 | + s.append("<time:").append(System.currentTimeMillis() - start).append(">"); | |
| 44 | + logger.info(s.toString()); | |
| 45 | + } | |
| 46 | + | |
| 47 | + private String getParams(HttpServletRequest request) throws JsonProcessingException { | |
| 48 | + Map<String, String[]> params = request.getParameterMap(); | |
| 49 | + return mapper.writeValueAsString(params); | |
| 50 | + } | |
| 51 | + | |
| 52 | + private String getBlock(Object msg) { | |
| 53 | + if (msg == null) { | |
| 54 | + msg = ""; | |
| 55 | + } | |
| 56 | + return "[" + msg.toString() + "]"; | |
| 57 | + } | |
| 58 | +} | ... | ... |
src/main/java/com/bsth/util/IpUtils.java
0 → 100644
| 1 | +package com.bsth.util; | |
| 2 | + | |
| 3 | +import javax.servlet.http.HttpServletRequest; | |
| 4 | + | |
| 5 | +public class IpUtils { | |
| 6 | + | |
| 7 | + public static String getIpAddr(HttpServletRequest request) { | |
| 8 | + if (request == null) { | |
| 9 | + return "unknown"; | |
| 10 | + } | |
| 11 | + String ip = request.getHeader("x-forwarded-for"); | |
| 12 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 13 | + ip = request.getHeader("Proxy-Client-IP"); | |
| 14 | + } | |
| 15 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 16 | + ip = request.getHeader("X-Forwarded-For"); | |
| 17 | + } | |
| 18 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 19 | + ip = request.getHeader("WL-Proxy-Client-IP"); | |
| 20 | + } | |
| 21 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 22 | + ip = request.getHeader("X-Real-IP"); | |
| 23 | + } | |
| 24 | + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |
| 25 | + ip = request.getRemoteAddr(); | |
| 26 | + } | |
| 27 | + return ip; | |
| 28 | + } | |
| 29 | +} | ... | ... |