标签:return hash end otn run 效果 dispatch text shm
Servlet30WrapperFilter主要是将原始请求进行包装,将原始的HttpServletRequest请求包装成Servlet30RequestWrapper类型的。
/***/ public class Servlet30WrapperFilter extends ZuulFilter {
private Field requestField = null; /***/ public Servlet30WrapperFilter() { /**ReflectionUtils.findField:在HttpServletRequestWrapper中查询字段名为req,字段类型为HttpServletRequest的字段属性*/ this.requestField = ReflectionUtils.findField(HttpServletRequestWrapper.class, "req", HttpServletRequest.class); Assert.notNull(this.requestField, "HttpServletRequestWrapper.req field not found"); this.requestField.setAccessible(true); } /**获取HttpServletRequest类型的字段属性req*/ protected Field getRequestField() { return this.requestField; } public String filterType() { return "pre"; } public int filterOrder() { return -2; } public boolean shouldFilter() { return true; } /***/ public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); /**判断请求是否是HttpServletRequestWrapper类型的;原始的请求一般不是它的子类
*ReflectionUtils.getField:在指定对象上得到指定的属性值
*这里是从request中取得req属性的值;其中req字段的类型是HttpServletRequest
*这里的实际作用就是将HttpServletRequestWrapper类型的请求转换成Servlet30RequestWrapper的类型;简单而言就是换了层外壳
*/ if (request instanceof HttpServletRequestWrapper) { request = (HttpServletRequest)ReflectionUtils.getField(this.requestField, request); ctx.setRequest(new Servlet30RequestWrapper(request)); } else if (RequestUtils.isDispatcherServletRequest()) {
/**
*判断请求request是否是从DispatcherServlet进来的,是的话就包装它
*
*/ ctx.setRequest(new Servlet30RequestWrapper(request)); } return null; } }
本过滤器实际就是将原始请求包装成Servlet30RequestWrapper类型的,扩展其功能。
包装后的效果:等同于HttpServletRequestWrapper包装。
1 class Servlet30RequestWrapper extends HttpServletRequestWrapper { 2 private HttpServletRequest request; 3 4 Servlet30RequestWrapper(HttpServletRequest request) { 5 super(request); 6 this.request = request; 7 } 8 9 public HttpServletRequest getRequest() { 10 return this.request; 11 } 12 }
扩展的(部分)内容有:
/***在HttpServletReuqestWrapper中*/
private static final HashMap<String, String[]> EMPTY_MAP = new HashMap();
private byte[] contentData = null; private HashMap<String, String[]> parameters = null; private long bodyBufferingTimeNs = 0L;
从上述属性可以看出,经过包装之后可以修改请求的一些参数,以及contentData等内容。
pre过滤器——Servlet30WrapperFilter
标签:return hash end otn run 效果 dispatch text shm
原文地址:https://www.cnblogs.com/AWSL/p/10631229.html