基本的思路是在请求处理之前通过 HandlerInterceptor 拦截,将 HttpServletRequest 中的数据获取到并缓存在内存中,再通过框架提供的配置接口将数据 Set 回去
因此总结分为两个部分处理:
拦截请求
缓存请求数据并重新设置回去
代码实现
缓存请求并重新设置回请求对象
1 2 3 4 5 6 7 8 9 10 11
/** * Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. * * <p> * This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object. * * @see javax.servlet.http.HttpServletRequest * @since Servlet 2.3 */ publicclassHttpServletRequestWrapperextendsServletRequestWrapperimplementsHttpServletRequest {}