Java Web 服务 HttpServletRequestWrapper 问题:IllegalStateException
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在对我的问题的评论中所述:
但我发现这个解决方案非常好(使用过滤器)而不是雄猫阀。
As stated in my comment to my question:
I found however that this solution is quite nice (using a filter) instead of a tomcat valve.