首页 > 代码库 > 01. localhost_access_log 记录post请求参数

01. localhost_access_log 记录post请求参数

环境:apache-tomcat-7.0.57

利用Filter过去request请求参数

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
package com.sea.lhsaq2009;
 
import java.io.IOException;
import java.util.Enumeration;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
 
public final class PostDataDumperFilter implements Filter {
 
    private FilterConfig filterConfig = null;
 
    @Override
    public void destroy() {
        this.filterConfig = null;
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        if(!req.getMethod().equals("GET")){ // Just GET or POST
            if (filterConfig == null) {
                return;
            }
            Enumeration<String> names = request.getParameterNames();
            StringBuffer output = new StringBuffer();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                output.append(name + "=");
                String values[] = request.getParameterValues(name);
                for (int i = 0; i < values.length; i++) {
                    if (i > 0) {
                        output.append(",");
                    }
                    output.append(values[i]);
                }
                if (names.hasMoreElements()) {
                    output.append("&");
                }
            }
            req.setAttribute("postdata", output);
        }
        chain.doFilter(request, response);
    }
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
}

web.xml中配置该Filter

1
2
3
4
5
6
7
8
<filter>
    <filter-name>post-data-dumper-filter</filter-name>
    <filter-class>com.sea.lhsaq2009.PostDataDumperFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>post-data-dumper-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

修改tomcat配置文件server.xml

修改 Host标签 下的 value标签
1
2
3
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
        prefix="localhost_access_log." suffix=".txt" 
        pattern=‘%h %p %H %l %u %t "%r" params={%{postdata}r} %s %bbytes %Dms‘ resolveHosts="false"/>

打印结果

1
2
127.0.0.1 8080 HTTP/1.1 - - [17/May/2017:23:27:46 +0800] "GET /sayHello/aaa?name=linghaisen&name=linghaisen2&age=18&sex=1&phone=13863683838 HTTP/1.1" params={-} 404 949bytes 2ms
127.0.0.1 8080 HTTP/1.1 - - [17/May/2017:23:19:27 +0800] "POST /sayHello/aaa HTTP/1.1" params={name=linghaisen,linghaisen2&age=18&sex=1&phone=13863683838} 404 949bytes 3ms
2017年5月17日 23:32:48

01. localhost_access_log 记录post请求参数