解决SpringMVC拦截器中Request数据只能读取一次的问题
开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request.getParameter()从Request中取数据。
但是有时候我们要在请求到具体的业务之前做一些操作比如日志记录、数据校验、统一的处理等等,可以在拦截器中处理。
由于 request中getReader()和getInputStream()只能调用一次,我们在拦截器中获取Request中数据后,后面就没法在继续获取数据了,那么可以重写HttpServletRequestWrapper方法来解决。
1. 重写HttpServletRequestWrapper方法:
package com.centit.server.common.utils; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /** * <p>request包装类<p> * @version 1.0 * @author li_hao * @date 2019年3月3日 */ public class RequestWrapper extends HttpServletRequestWrapper { private final String body; public RequestWrapper(HttpServletRequest request) throws IOException { super(request); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = request.getInputStream(); if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, 0, bytesRead); } } else { stringBuilder.append(""); } } catch (IOException ex) { throw ex; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ex) { throw ex; } } } body = stringBuilder.toString(); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); ServletInputStream servletInputStream = new ServletInputStream() { public boolean isFinished() { return false; } public boolean isReady() { return false; } public void setReadListener(ReadListener readListener) {} public int read() throws IOException { return byteArrayInputStream.read(); } }; return servletInputStream; } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(this.getInputStream())); } public String getBody() { return this.body; } }
2. 拦截器
package com.centit.server.common.interceptor; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Collections; import java.util.HashSet; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.centit.server.common.utils.RequestWrapper; /** * <p>拦截器<p> * @version 1.0 * @author li_hao * @date 2019年3月3日 */ public class HandlerInterceptor extends HandlerInterceptorAdapter{ public static Set<HttpSession> sessions; static{ if(sessions==null){ sessions = Collections.synchronizedSet(new HashSet<HttpSession>()); } } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String uri = request.getRequestURI(); //获取请求方法:格式为:/meetingserv_xa/smDept/queryAllSmDeptTreeList String contextpath = request.getContextPath(); System.out.println(uri); System.out.println(contextpath); String ipAddr = getIpAddr(request); //获取客户端ip System.out.println(ipAddr); RequestWrapper myRequestWrapper = new RequestWrapper((HttpServletRequest) request); String body = myRequestWrapper.getBody(); System.out.println(body); //请求数据 return super.preHandle(request, response, handler); } /** * 获取客户端请求的当前网络ip * @param request * @return */ public String getIpAddr(HttpServletRequest request){ String ipAddress = request.getHeader("x-forwarded-for"); if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){ //根据网卡取本机配置的IP InetAddress inet=null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ipAddress= inet.getHostAddress(); } } //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 if(ipAddress.indexOf(",")>0){ ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); } } return ipAddress; } }
3. 配置拦截器(spring-mvc.xml):
<!-- 拦截器 -->
<mvc:interceptors>
<bean class="com.centit.server.common.interceptor.HandlerInterceptor" />
</mvc:interceptors>
4. 过滤器:
package com.centit.server.common.filter; import java.io.IOException; 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; import com.centit.server.common.utils.RequestWrapper; /** * <p>过滤器<p> * @version 1.0 * @author li_hao * @date 2019年3月3日 */ public class HttpServletFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletRequest requestWrapper = null; if(request instanceof HttpServletRequest) { requestWrapper = new RequestWrapper((HttpServletRequest) request); } if(requestWrapper == null) { chain.doFilter(request, response); } else { chain.doFilter(requestWrapper, response); } } public void destroy() { } }
5. 配置过滤器(web.xml):
<filter> <filter-name>requestFilter</filter-name> <filter-class>com.centit.server.common.filter.HttpServletFilter</filter-class> </filter> <filter-mapping> <filter-name>requestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>