简体   繁体   English

多次读取请求流

[英]Read the request stream multiple times

We have a Tomcat ValveBase class implementation that is doing the authentication for our servlet container apps. 我们有一个Tomcat ValveBase类实现,正在对我们的servlet容器应用程序进行身份验证。 One way to authenticate our http REST calls is to sign them and then check the signature on the server side. 验证我们的http REST调用的一种方法是对它们签名,然后在服务器端检查签名。 We do this check in the ValveBase class. 我们在ValveBase类中进行此检查。

The problem is that after we consume the InputStream of the request (for validating the signature), we pass the request (org.apache.catalina.connector.Request) to the next valve implementation and by the time it hits the servlet, the inputStream is gone. 问题在于,在消耗了请求的InputStream(用于验证签名)之后,我们将请求(org.apache.catalina.connector.Request)传递到下一个Valve实现,并在到达servlet时将其传递给inputStream离开了。 No content to be delivered, since it was consumed at the signature verification procedure. 没有内容要传送,因为它是在签名验证过程中消耗的。

In the javax.servlet api, you can use an HttpServletRequestWrapper to implement your own ServletRequest and pass the real request as a constructor argument. 在javax.servlet api中,可以使用HttpServletRequestWrapper来实现自己的ServletRequest并将实际请求作为构造函数参数传递。 In that case, we were able to avoid the situation where the content was read only once, but in the case of the catalina Request, seemed to be more delicate than we thought. 在那种情况下,我们可以避免只读取一次内容的情况,但是在“ catalina请求”的情况下,似乎比我们想象的要复杂。 Any ideas? 有任何想法吗? Thanks. 谢谢。

There is a long standing enhancement request open against Tomcat to support wrappers for use in Valves in a similar manner to Filters. 长期以来,针对Tomcat 提出增强请求,要求以类似于过滤器的方式支持在Valve中使用包装器。

The bug includes a patch that is likely to need updating for Tomcat 7.0.x. 该错误包含一个补丁,可能需要为Tomcat 7.0.x更新。

Given that you are already using a custom valve adding the patch may not be too much of a leap. 鉴于您已经在使用自定义阀,因此添加补丁可能不会有太大的飞跃。 With that patch in place, you should be able wrap the internal Request object and solve this problem in a similar manner to the Filter solution (which I assume involves saving a copy of the request body - watch out for DoS issues). 有了该补丁程序之后,您应该可以包装内部Request对象,并以与Filter解决方案类似的方式解决此问题(我认为该解决方案涉及保存请求正文的副本-注意DoS问题)。

This is, of course, completely untested. 当然,这是完全未经测试的。 As an incentive to try it, if it does work and you provide the updated patch (attach it to the Bugzilla report) I'll look at getting it included in Tomcat 8.0.x and 7.0.x (providing it doesn't require any changes to the existing API). 作为一种尝试,如果它可以工作并且您提供了更新的补丁(将其附加到Bugzilla报告中),作为激励,我将尝试将其包含在Tomcat 8.0.x和7.0.x中(前提是它不需要任何更改现有的API)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM