Java Web 服务 HttpServletRequestWrapper 问题:IllegalStateException

发布于 2024-10-17 05:48:28 字数 1330 浏览 2 评论 0原文

我目前正在使用 Jersey 库在 Java 中开发 RESTful Web 服务。

出于安全原因,我们需要类似于 Amazon 简单存储服务的自定义身份验证。然而,这需要我计算主体的 MD5 哈希值(如果有的话)来验证请求。

到目前为止,我已经使用了自定义的身份验证器和领域并将它们插入到我的上下文中。 在尝试计算哈希值时,我首先使用请求本身,导致 IllegalStateException,因为主体只能读取一次。

在调查问题之后,我尝试将请求包装在 HttpServletRequestWrapper 中,但到目前为止尚未成功。

我基本上使用如下所示的包装器: http://forums.oracle.com/forums/thread.jspa ?threadID=2156814&tstart=0

在我的领域内,我进行身份验证,我首先像这样创建包装器:

MyRequestWrapper requestWrapper = new MyRequestWrapper(request);

然后我使用 requestWrapper 计算 MD5 最后转发它

request.getRequestDispatcher("/*").forward(requestWrapper, response);

处理工作正常,但之后我收到这样的错误:

Servlet.service() for servlet Jersey REST Service threw exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
    at com.sun.jersey.spi.container.servlet.WebComponent$Writer.finish(WebComponent.java:285)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:241)

请注意,没有提及之前调用的 getReader 或 getInputStream (就像我根本没有使用任何包装器一样)。

现在我确信我在这里做错了什么,但我真的对此了解不多,如果有人可以在这里帮助我,我会非常高兴:)

最好的问候, 卢卡斯

I am currently developing a RESTful Webservice in Java using the Jersey library.

For security reasons, we want a custom authentication similar to Amazons Simple Storage Service. This requires, however, that I calculate an MD5 hash of the body (if there is any) to authenticate the request.

So far, I have used a custom Authenticator and Realm and plugged them into my context.
Upon trying to calculate the hash I first used the request itself resulting in an IllegalStateException, since the body can only be read once.

After investigating the problem I tried to wrap the request inside a HttpServletRequestWrapper but hasn't been successful so far.

I am basically using a wrapper like the one shown here:
http://forums.oracle.com/forums/thread.jspa?threadID=2156814&tstart=0

Inside my realm, where I do the authentication, I am first creating the wrapper like so:

MyRequestWrapper requestWrapper = new MyRequestWrapper(request);

then I am calculating the MD5 using the requestWrapper
and finally forwarding it

request.getRequestDispatcher("/*").forward(requestWrapper, response);

The processing works fine but I get an error like this after that:

Servlet.service() for servlet Jersey REST Service threw exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
    at com.sun.jersey.spi.container.servlet.WebComponent$Writer.finish(WebComponent.java:285)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:241)

Note that there is no mentioning of the getReader or getInputStream being called before (like I got without using any wrapper at all).

Now I am sure I am doing something wrong here but I really don't know much about this and would be really glad if someone could help me out here :)

Best Regards,
Lukas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-10-24 05:48:28

正如我在对我的问题的评论中所述:

我正在从请求访问 getReader() 。回复我没碰。但是我发现问题出在转发包装器上。我没有在我的问题中明确说明这一点,但我正在使用 tomcat 并尝试在阀门内使用上述代码。我仍然对阀门是否可以实现这一点感兴趣,因为这更适合 tomcat 模型。我现在开始使用过滤器,虽然不太好,但很有效

但我发现这个解决方案非常好(使用过滤器)而不是雄猫阀。

As stated in my comment to my question:

I was accessing getReader() from the request. Response I did not touch. However I found that the problem was forwarding the wrapper. I didn't explicitly state this in my question but I am using tomcat and tried to use the above code inside a valve. I am still interested in the question if this is also possible from a valve, since this fits better into the tomcat model. I have now moved to using a filter which is not so nice, but works

I found however that this solution is quite nice (using a filter) instead of a tomcat valve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文