1. 程式人生 > >攔截請求引數springmvc

攔截請求引數springmvc

@Component
@Slf4j
public class wxgtest extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        // 所有請求第一個進入的方法
        String reqURL = request.getRequestURL().toString();
        String ip = request.getRemoteHost();

        InputStream is = request.getInputStream();
        StringBuilder responseStrBuilder = new StringBuilder();
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null) { responseStrBuilder.append(inputStr); }
        //      System.out.println("請求引數: " + responseStrBuilder.toString ());
        String parmeter = responseStrBuilder.toString();

        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        if (handler instanceof HandlerMethod) {
            StringBuilder sb = new StringBuilder(1000);
            sb.append("-----------------------").append(DateUtil.getCurrentDateStamp()).append(
                "-------------------------------------\n");
            HandlerMethod h = (HandlerMethod)handler;
            //Controller 的包名
            sb.append("Controller: ").append(h.getBean().getClass().getName()).append("\n");
            //方法名稱
            sb.append("Method    : ").append(h.getMethod().getName()).append("\n");
            //請求方式  post\put\get 等等
            sb.append("RequestMethod    : ").append(request.getMethod()).append("\n");
            //所有的請求引數
            sb.append("Params    : ").append(parmeter).append("\n");
            //部分請求連結
            sb.append("URI       : ").append(request.getRequestURI()).append("\n");
            //完整的請求連結
            sb.append("AllURI    : ").append(reqURL).append("\n");
            //請求方的 ip地址
            sb.append("request IP: ").append(ip).append("\n");
            System.out.println(sb.toString());
            LogUtil.info(log, sb.toString());

        }
        //       修改request中的引數並儲存到request中
        request.setAttribute("parmeter_json", parmeter);

        return true;
    }

}

 

加入

registry.addInterceptor(this.wxgtest).addPathPatterns(new String[]{"/**"});