增强request对象的时候 如果配置了dispatcherTypes的话 会将requestFaced对象增强为ApplicationHttpRequest对象,那么在获取真实对象的interfaces() 接口数组的时候,长度为0.
(ApplicationHttpRequest extends HttpServletRequestWrapper、HttpServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper implements HttpServletRequest 、HttpServletRequest extends javax.servlet.http.HttpServletRequest、interface HttpServletRequest extends ServletRequest)如图所示
(打开该图的快捷键为 shift+ctrl+alt+u)
原因是因为 req.getClass().getInterfaces() 获取父接口数组为空。改为
new Class[]{Class.forName(“javax.servlet.http.HttpServletRequest”)}
指定接口就可以了。
设置@WebFilter(value = “/*”,dispatcherTypes = DispatcherType.FORWARD) 后,tomcat会产生一个增强的request对象ApplicationHttpRequest
但是比较奇怪的是,这个对象是HttpServletRequest的实现类对象,但是获取不了父接口…我还得查查。
通过测试我的版本为:
ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), org.apache.catalina.servlet4preview.http.HttpServletRequest.class.getInterfaces(), new InvocationHandler();
也就是说,只要将第二个参数替换为指定的、能够获取到父结构数组的类型即可(所以此处有多种取值 可以根据图上所示自主验证)