java-filter打印請(qǐng)求返回參數(shù)_第1頁(yè)
java-filter打印請(qǐng)求返回參數(shù)_第2頁(yè)
java-filter打印請(qǐng)求返回參數(shù)_第3頁(yè)
java-filter打印請(qǐng)求返回參數(shù)_第4頁(yè)
java-filter打印請(qǐng)求返回參數(shù)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1、對(duì) HttpServletRequestWrapper進(jìn)行封裝,先把數(shù)據(jù)保存到臨時(shí)byte,然后得到數(shù)據(jù)后,再把數(shù)據(jù)復(fù)制到request中2、對(duì)HttpServletResponseWrapper進(jìn)行封裝,同樣道理把數(shù)據(jù)輸出到ByteArrayOutputStream,得到數(shù)據(jù)后,再response.getWriter().write(result);3、 filter中對(duì)requestresponse封裝,filter.doFilter(requestWrapper, responseWrapper);還需要在web.xml配置 filter4、亂碼解決:responseWrapper中

2、輸出流設(shè)置編碼,response中也設(shè)置編碼即可解決亂碼問(wèn)題重要補(bǔ)充:對(duì)經(jīng)過(guò) filter 后 Enumeration ks=request.getParameterNames();無(wú)法獲取正確參數(shù)的問(wèn)題,查看 tomcat 對(duì) request 參數(shù)的處理,解決方式為覆蓋HttpServletRequestWrapper 中的getParameterNames()和 getParameter()函數(shù),因?yàn)閠omcat 讀取一次請(qǐng)求參數(shù)request.getParameterNames()后就將原參數(shù)歸 0,所以需要覆蓋原 HttpServletRequestWrapper 中的部分函數(shù)。5、R

3、equestWrapper如下package com.niuwa.p2p.filter;/* date 2016年 9 月 1 日 上午 10:24:05*/import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import jav

4、a.util.LinkedHashMap;import java.util.Map;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import org.apache.catalina.connector.RequestFacade;import org.apache.http.message.BasicNameValuePair;import com.alibaba.f

5、astjson.JSON;import com.itextpdf.text.List;import jodd.io.StreamUtil;/* 復(fù)制 request 中的 bufferedReader中的值* date 2016年 9 月 1 日 上午 10:53:06*/public class RequestWrapper extends HttpServletRequestWrapper private final byte body;private final Map paramHashValues = new LinkedHashMap();private boolean didQu

6、eryParameters=false; /參考o(jì)rg.apache.tomcat.util.http.Parameters/用 tomcat 建立一個(gè)普通的Java web ,out.println(request)-結(jié)果為:org.apache.catalina.connector.RequestFacade149d226/用 strut2 框架的話, out.println(request)-結(jié)果為:org.apache.struts2.dispatcher.StrutsRequestWrapper1e17cff/Enumeration ks=request.getParameterNa

7、mes();/while(ks.hasMoreElements()/String key=ks.nextElement();/String value=request.getParameter(key);/nvps.add(new BasicNameValuePair(key, value);/(key:+key+,value:+value);/* 這個(gè)必須加 ,復(fù)制 request 中的 bufferedReader中的值* param request* throws IOException*/public RequestWrapper(HttpServletReque

8、st request) throws IOException super(request);initParamHashValues();body = StreamUtil.readBytes(request.getReader(), UTF-8);/這一句和上一句同時(shí)只能有一個(gè)Overridepublic BufferedReader getReader() throws IOException return new BufferedReader(new InputStreamReader(getInputStream();Overridepublic ServletInputStream g

9、etInputStream() throws IOException final ByteArrayInputStream bais = new ByteArrayInputStream(body);return new ServletInputStream() Overridepublic int read() throws IOException return bais.read();Overridepublic Enumeration getParameterNames() initParamHashValues();return Collections.enumeration(para

10、mHashValues.keySet();Overridepublic String getParameter(String name) initParamHashValues();return (String) paramHashValues.get(name);public void initParamHashValues()if (!didQueryParameters) didQueryParameters=true;Enumeration ks=super.getParameterNames();while(ks.hasMoreElements()String key=ks.next

11、Element();String value=super.getParameter(key);paramHashValues.put(key, value);System.out.println(initParamHashValues:+JSON.toJSON(paramHashValues);6、 ResponseWrapperimport java.io.ByteArrayOutputStream;import java.io.CharArrayWriter;import java.io.IOException;import java.io.OutputStreamWriter;impor

12、t java.io.PrintWriter;import java.io.UnsupportedEncodingException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;public class ResponseWrapper extends HttpServletResponseWrapper /*This class implements an out

13、put stream in which the data is written intoa byte array.The buffer automatically grows as data is written to it. The data can beretrieved using toByteArray() and toString().Closing a ByteArrayOutputStream has no effect. The methods in this class can becalled after the stream has been closed without

14、 generating an IOException.*/private ByteArrayOutputStream buffer = null;/輸出到byte arrayprivate ServletOutputStream out = null;private PrintWriter writer = null;public ResponseWrapper(HttpServletResponse resp) throws IOException super(resp);buffer = new ByteArrayOutputStream();/ 真正存儲(chǔ)數(shù)據(jù)的流 out = newWap

15、peredOutputStream(buffer);writer = new PrintWriter(new OutputStreamWriter(buffer,this.getCharacterEncoding();/*重載父類獲取outputstream的方法*/Overridepublic ServletOutputStream getOutputStream() throws IOException return out;/*重載父類獲取writer的方法 */Overridepublic PrintWriter getWriter() throws UnsupportedEncodi

16、ngException return writer;/*重載父類獲取flushBuffer的方法*/Overridepublic void flushBuffer() throws IOException if (out != null) out.flush();if (writer != null) writer.flush();Overridepublic void reset() buffer.reset();/*將 out 、writer中的數(shù)據(jù)強(qiáng)制輸出到WapperedResponse 的 buffer里面,否則取不到數(shù)據(jù) */public byte getResponseData(

17、) throws IOException flushBuffer();return buffer.toByteArray();/*內(nèi)部類,對(duì)ServletOutputStream進(jìn)行包裝*/private class WapperedOutputStream extends ServletOutputStream private ByteArrayOutputStream bos = null;public WapperedOutputStream(ByteArrayOutputStream stream) throwsIOException bos = stream;Overridepubl

18、ic void write(int b) throws IOException bos.write(b);Overridepublic void write(byte b) throws IOException bos.write(b, 0, b.length);7、 UrlFilter/*url過(guò)濾器 , 防止暴露接口訪問(wèn)方式*/import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import java.util.Map;import javax.servlet.Filter;

19、import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResp

20、onse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class UrlFilter implements Filter static Logger logger = LoggerFactory.getLogger(UrlFilter.class);ServletContext context;Overridepublic void destroy() Overridepublic void doFilter(ServletRequest request, ServletResponse response,Filt

21、erChain filter) throws IOException, ServletException HttpServletRequest r = (HttpServletRequest) request;String path = r.getQueryString();String url = r.getRequestURI();RequestWrapper requestWrapper = null;String repalceUrl = url.replaceAll(/, );if (null != repalceUrl) repalceUrl = repalceUrl.trim()

22、; else return;(nn-path);url: + url + & queryString: +ResponseWrapperresponseWrapper=newResponseWrapper(HttpServletResponse)response);if (request instanceof HttpServletRequest) requestWrapper = new RequestWrapper(HttpServletRequest) request);try Map map = request.getParameterMap();logger.i

23、nfo(map: + map);BufferedReader bufferedReader = requestWrapper.getReader();String line;StringBuilder sb = new StringBuilder();while (line = bufferedReader.readLine() != null) sb.append(line);(request.getReader: + sb.toString(); catch (Exception e) TODO: handle exceptionlogger.error(p2ps doFilter :, e);if (null = requestWrapper) filter.doFilter(request, response); else filter.doFil

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論