HttpServletRequestWrapper使用技巧(自定义session和缓存InputStream)

一、前言

  javax.servlet.http.HttpServletRequestWrapper 是一个开发者可以继承的类,我们可以重写相应的方法来实现session的自定义以及缓存InputStream,在程序中可以多次获取request body的内容。

二、自定义seesion

import javax.servlet.http.*;public class CustomizeHttpServletRequest extends HttpServletRequestWrapper {public CustomizeHttpServletRequest(HttpServletRequest request) {super(request);this.response = response;}@Overridepublic HttpSession getSession() {//return super.getSession(); // 默认使用的是servlet容器session管理return this.getSession(true);}@Overridepublic HttpSession getSession(boolean create) {Cookie[] cookies = this.getCookies();String sessionId = "";//这里编写自己获取session的逻辑//既然是自定义逻辑,可以从内存中取,也可以从缓存中取。
    }
}

  也许大家都用过shiro的session管理或者spring-session,其实想要自己去实现,也是很简单的。

三、缓存InputStream

  自定义工具类 ContentCachingRequestWrapper 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;import org.apache.commons.io.IOUtils;public class ContentCachingRequestWrapper extends HttpServletRequestWrapper{private byte[] body;private BufferedReader reader;private ServletInputStream inputStream;public ContentCachingRequestWrapper(HttpServletRequest request) throws IOException{super(request);body = IOUtils.toByteArray(request.getInputStream());inputStream = new RequestCachingInputStream(body);}public byte[] getBody() {return body;}@Overridepublic ServletInputStream getInputStream() throws IOException {if (inputStream != null) {          return inputStream;}return super.getInputStream();}@Overridepublic BufferedReader getReader() throws IOException {if (reader == null) {reader = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding()));}return reader;}private static class RequestCachingInputStream extends ServletInputStream {private final ByteArrayInputStream inputStream;public RequestCachingInputStream(byte[] bytes) {inputStream = new ByteArrayInputStream(bytes);}@Overridepublic int read() throws IOException {return inputStream.read();}@Overridepublic boolean isFinished() {return inputStream.available() == 0;}@Overridepublic boolean isReady() {return true;}@Overridepublic void setReadListener(ReadListener readlistener) {}}}

  spring工具类 ContentCachingRequestWrapper 

package org.springframework.web.util;import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.http.HttpMethod;public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";private final ByteArrayOutputStream cachedContent;private ServletInputStream inputStream;private BufferedReader reader;/*** Create a new ContentCachingRequestWrapper for the given servlet request.* @param request the original servlet request*/public ContentCachingRequestWrapper(HttpServletRequest request) {super(request);int contentLength = request.getContentLength();this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ? contentLength : 1024);}@Overridepublic ServletInputStream getInputStream() throws IOException {if (this.inputStream == null) {this.inputStream = new ContentCachingInputStream(getRequest().getInputStream());}return this.inputStream;}@Overridepublic String getCharacterEncoding() {String enc = super.getCharacterEncoding();return (enc != null ? enc : WebUtils.DEFAULT_CHARACTER_ENCODING);}@Overridepublic BufferedReader getReader() throws IOException {if (this.reader == null) {this.reader = new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding()));}return this.reader;}@Overridepublic String getParameter(String name) {if (this.cachedContent.size() == 0 && isFormPost()) {writeRequestParametersToCachedContent();}return super.getParameter(name);}@Overridepublic Map<String, String[]> getParameterMap() {if (this.cachedContent.size() == 0 && isFormPost()) {writeRequestParametersToCachedContent();}return super.getParameterMap();}@Overridepublic Enumeration<String> getParameterNames() {if (this.cachedContent.size() == 0 && isFormPost()) {writeRequestParametersToCachedContent();}return super.getParameterNames();}@Overridepublic String[] getParameterValues(String name) {if (this.cachedContent.size() == 0 && isFormPost()) {writeRequestParametersToCachedContent();}return super.getParameterValues(name);}private boolean isFormPost() {String contentType = getContentType();return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) &&HttpMethod.POST.matches(getMethod()));}private void writeRequestParametersToCachedContent() {try {if (this.cachedContent.size() == 0) {String requestEncoding = getCharacterEncoding();Map<String, String[]> form = super.getParameterMap();for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) {String name = nameIterator.next();List<String> values = Arrays.asList(form.get(name));for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) {String value = valueIterator.next();this.cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());if (value != null) {this.cachedContent.write('=');this.cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes());if (valueIterator.hasNext()) {this.cachedContent.write('&');}}}if (nameIterator.hasNext()) {this.cachedContent.write('&');}}}}catch (IOException ex) {throw new IllegalStateException("Failed to write request parameters to cached content", ex);}}/*** Return the cached request content as a byte array.*/public byte[] getContentAsByteArray() {return this.cachedContent.toByteArray();}private class ContentCachingInputStream extends ServletInputStream {private final ServletInputStream is;public ContentCachingInputStream(ServletInputStream is) {this.is = is;}@Overridepublic int read() throws IOException {int ch = this.is.read();if (ch != -1) {cachedContent.write(ch);}return ch;}@Overridepublic boolean isFinished() {return is.available() == 0;}@Overridepublic boolean isReady() {return true;}@Overridepublic void setReadListener(ReadListener readlistener) {}}}

  获取InputStream

  1、使用自定义工具类的时候调用方法 getBody

  2、使用spring工具类的时候调用方法 getContentAsByteArray

  打印request中的所有请求信息,详细代码如下。

private void printRequest(HttpServletRequest request) {String body = StringUtils.EMPTY;try {if (request instanceof ContentCachingRequestWrapper) {body = new String(((ContentCachingRequestWrapper) request).getContentAsByteArray(), "UTF-8");LOGGER.info("Request-Inputstream: " + body);}} catch (IOException e) {LOGGER.error("printRequest 获取body异常...", e);}JSONObject requestJ = new JSONObject();JSONObject headers = new JSONObject();Collections.list(request.getHeaderNames()).stream().forEach(name -> headers.put(name, request.getHeader(name)));requestJ.put("headers", headers);requestJ.put("parameters", request.getParameterMap());requestJ.put("body", body);requestJ.put("remote-user", request.getRemoteUser());requestJ.put("remote-addr", request.getRemoteAddr());requestJ.put("remote-host", request.getRemoteHost());requestJ.put("remote-port", request.getRemotePort());requestJ.put("uri", request.getRequestURI());requestJ.put("url", request.getRequestURL());requestJ.put("servlet-path", request.getServletPath());requestJ.put("method", request.getMethod());requestJ.put("query", request.getQueryString());requestJ.put("path-info", request.getPathInfo());requestJ.put("context-path", request.getContextPath());LOGGER.info("Request-Info: " + JSON.toJSONString(requestJ, SerializerFeature.PrettyFormat));
}

  request中的所有请求信息示例

 Request-Inputstream: {"timestamp":1539155028668,"appId":"cmos10086e36ipz2otyy8gfqh","nonce":691879,"telephone":"18736085778","signature":"226e734a49d513b3b1e364a06fc6f4eb5e2c425c6446ce6a7a950f1d8d6af06c"
}Request-Info: {"headers":{"x-real-ip":"211.138.20.171","content-length":"183","content-encoding":"UTF-8","host":"221.176.66.251","connection":"close","content-type":"application/json","accept-encoding":"gzip,deflate","user-agent":"Apache-HttpClient/4.5.3 (Java/1.7.0_76)"},"remote-host":"172.17.20.92","method":"POST","body":"{\"timestamp\":1539155028668,\"appId\":\"cmos10086e36ipz2otyy8gfqh\",\"nonce\":691879,\"telephone\":\"18736085778\",\"signature\":\"226e734a49d513b3b1e364a06fc6f4eb5e2c425c6446ce6a7a950f1d8d6af06c\"}","uri":"/wmhopenapi/hevb-api/total","url":"http://221.176.66.251/wmhopenapi/hevb-api/total","servlet-path":"/hevb-api/total","remote-addr":"172.17.20.92","context-path":"/wmhopenapi","remote-port":49174,"parameters":{}
}

四、在Filter中替换掉默认的Request

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.util.ContentCachingRequestWrapper;import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;@Configuration
public class FilterConfig {@Beanpublic FilterRegistrationBean wmhFilterRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new WmhFilter());registration.addUrlPatterns("/*");registration.setName("MyFilter");registration.setOrder(1);return registration;}private static class WmhFilter implements Filter {@Overridepublic void init(javax.servlet.FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {chain.doFilter(new ContentCachingRequestWrapper((HttpServletRequest) request), response);}@Overridepublic void destroy() {}}
}

 五、使用场景

  个人认为,在做权限管理、用户管理、登录等场景,尤其是多系统的情况下,常常需要借助第三方的工具比如shiro,spring-session,完成权限、角色、用户、登录的管理逻辑。之前我自己也尝试过使用spring-session+redis缓存实现共享session和单点登录的逻辑。如果时间充分的话,完全可以自己去写一套session管理的工具,并应用到项目中去。

  最近在配合其他组的同时联调接口的时候,遇到了这样的一种情况:他说request body的内容是按照我的协议来的,我后端的实现是通过@RequestBody注解拿到的一个java 对象,有一个字段值为null,很是诡异。于是我俩就纠结是谁的问题,我说他参数传的不对,他说我字段定义不对并让我打印一下Request InputStream,于是就开始寻找解决方案。我们都知道Input Sream只能获取一次,再次获取一定会抛出异常。通过寻找HttpServletRequest的子类,发现了spring提供了这样一个缓存Input Stream内容的工具类,问题迎刃而解。

转载于:https://www.cnblogs.com/hujunzheng/p/9766739.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531218.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

spring注解工具类AnnotatedElementUtils和AnnotationUtils

一、前言 spring为开发人员提供了两个搜索注解的工具类&#xff0c;分别是AnnotatedElementUtils和AnnotationUtils。在使用的时候&#xff0c;总是傻傻分不清&#xff0c;什么情况下使用哪一个。于是我做了如下的整理和总结。 二、AnnotationUtils官方解释 功能 用于处理注解&…

windows系统nexus3安装和配置

一、前言 为什么要在本地开发机器上安装nexus&#xff1f;首先声明公司内部是有自己的nexus仓库&#xff0c;但是对上传jar包做了限制&#xff0c;不能畅快的上传自己测试包依赖。于是就自己在本地搭建了一个nexus私服&#xff0c;即可以使用公司nexus私服仓库中的依赖&#xf…

Springmvc借助SimpleUrlHandlerMapping实现接口开关功能

一、接口开关功能 1、可配置化&#xff0c;依赖配置中心 2、接口访问权限可控 3、springmvc不会扫描到&#xff0c;即不会直接的将接口暴露出去 二、接口开关使用场景 和业务没什么关系&#xff0c;主要方便查询系统中的一些状态信息。比如系统的配置信息&#xff0c;中间件的状…

log4j平稳升级到log4j2

一、前言 公司中的项目虽然已经用了很多的新技术了&#xff0c;但是日志的底层框架还是log4j&#xff0c;个人还是不喜欢用这个的。最近项目再生产环境上由于log4j引起了一场血案&#xff0c;于是决定升级到log4j2。 二、现象 虽然生产环境有多个结点分散高并发带来的压力&…

Springboot集成ES启动报错

报错内容 None of the configured nodes are available elasticsearch.yml配置 cluster.name: ftest node.name: node-72 node.master: true node.data: true network.host: 112.122.245.212 http.port: 39200 transport.tcp.port: 39300 discovery.zen.ping.unicast.hosts: [&…

高效使用hibernate-validator校验框架

一、前言 高效、合理的使用hibernate-validator校验框架可以提高程序的可读性&#xff0c;以及减少不必要的代码逻辑。接下来会介绍一下常用一些使用方式。 二、常用注解说明 限制说明Null限制只能为nullNotNull限制必须不为nullAssertFalse限制必须为falseAssertTrue限制必须为…

kafka-manager配置和使用

kafka-manager配置 最主要配置就是用于kafka管理器状态的zookeeper主机。这可以在conf目录中的application.conf文件中找到。 kafka-manager.zkhosts"my.zookeeper.host.com:2181" 当然也可以声明为zookeeper集群。 kafka-manager.zkhosts"my.zookeeper.host.co…

kafka告警简单方案

一、前言 为什么要设计kafka告警方案&#xff1f;现成的监控项目百度一下一大堆&#xff0c;KafkaOffsetMonitor、KafkaManager、 Burrow等&#xff0c;具体参考&#xff1a;kafka的消息挤压监控。由于本小组的项目使用的kafka集群并没有被公司的kafka-manager管理&#xff0c;…

RedisCacheManager设置Value序列化器技巧

CacheManager基本配置 请参考博文&#xff1a;springboot2.0 redis EnableCaching的配置和使用 RedisCacheManager构造函数 /*** Construct a {link RedisCacheManager}.* * param redisOperations*/ SuppressWarnings("rawtypes") public RedisCacheManager(RedisOp…

Nginx配置以及域名转发

工程中的nginx配置 #user nobody; worker_processes 24; error_log /home/xxx/opt/nginx/logs/error.log; pid /home/xxx/opt/nginx/run/nginx.pid;events {use epoll;worker_connections 102400; }http {include /home/xxx/opt/nginx/conf.d/mime.types;default_…

java接口签名(Signature)实现方案续

一、前言 由于之前写过的一片文章 &#xff08;java接口签名(Signature)实现方案 &#xff09;收获了很多好评&#xff0c;此次来说一下另一种简单粗暴的签名方案。相对于之前的签名方案&#xff0c;对body、paramenter、path variable的获取都做了简化的处理。也就是说这种方式…

支付宝敏感信息解密

支付宝官方解密文档&#xff1a;https://docs.alipay.com/mini/introduce/aes String response "小程序前端提交的";//1. 获取验签和解密所需要的参数 Map<String, String> openapiResult JSON.parseObject(response,new TypeReference<Map<String, St…

HashMap 源码阅读

前言 之前读过一些类的源码&#xff0c;近来发现都忘了&#xff0c;再读一遍整理记录一下。这次读的是 JDK 11 的代码&#xff0c;贴上来的源码会去掉大部分的注释, 也会加上一些自己的理解。 Map 接口 这里提一下 Map 接口与1.8相比 Map接口又新增了几个方法&#xff1a;   …

SpringMvc接口中转设计(策略+模板方法)

一、前言 最近带着两个兄弟做支付宝小程序后端相关的开发&#xff0c;小程序首页涉及到很多查询的服务。小程序后端服务在我司属于互联网域&#xff0c;相关的查询服务已经在核心域存在了&#xff0c;查询这块所要做的工作就是做接口中转。参考了微信小程序的代码&#xff0c;发…

SpringSecurity整合JWT

一、前言 最近负责支付宝小程序后端项目设计&#xff0c;这里主要分享一下用户会话、接口鉴权的设计。参考过微信小程序后端的设计&#xff0c;会话需要依靠redis。相关的开发人员和我说依靠Redis并不是很靠谱&#xff0c;redis在业务高峰期不稳定&#xff0c;容易出现问题&…

Springboot定时任务原理及如何动态创建定时任务

一、前言 上周工作遇到了一个需求&#xff0c;同步多个省份销号数据&#xff0c;解绑微信粉丝。分省定时将销号数据放到SFTP服务器上&#xff0c;我需要开发定时任务去解析文件。因为是多省份&#xff0c;服务器、文件名规则、数据规则都不一定&#xff0c;所以要做成可配置是有…

转载:ThreadPoolExecutor 源码阅读

前言 之前研究了一下如何使用ScheduledThreadPoolExecutor动态创建定时任务(Springboot定时任务原理及如何动态创建定时任务)&#xff0c;简单了解了ScheduledThreadPoolExecutor相关源码。今天看了同学写的ThreadPoolExecutor 的源码解读&#xff0c;甚是NB&#xff0c;必须转…

Spring BPP中优雅的创建动态代理Bean

一、前言 本文章所讲并没有基于Aspectj&#xff0c;而是直接通过Cglib以及ProxyFactoryBean去创建代理Bean。通过下面的例子&#xff0c;可以看出Cglib方式创建的代理Bean和ProxyFactoryBean创建的代理Bean的区别。 二、基本测试代码 测试实体类&#xff0c;在BPP中创建BppTest…

使用pdfBox实现pdf转图片,解决中文方块乱码等问题

一、引入依赖 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.13</version> </dependency> <dependency><groupId>org.apache.pdfbox</groupId><artif…

Spring异步调用原理及SpringAop拦截器链原理

一、Spring异步调用底层原理 开启异步调用只需一个注解EnableAsync Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Import(AsyncConfigurationSelector.class) public interface EnableAsync {/*** Indicate the async annotation type to be detec…