IDEA动态代理敏感文字过滤com.sun.proxy.$Proxy0 cannot be cast

增强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();
也就是说,只要将第二个参数替换为指定的、能够获取到父结构数组的类型即可(所以此处有多种取值 可以根据图上所示自主验证)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值