【发布时间】:2015-11-10 14:38:43
【问题描述】:
我有以下问题:
ContainerRequestFilter 是一个单例,但请阅读:
在第 9.2 章中,他们说:
上下文特定于特定请求,但某些 JAX-RS 组件的实例(提供者和具有除每个请求之外的生命周期的资源类)可能需要支持多个并发请求。当注入第 9.2 节中列出的类型之一的实例时,提供的实例必须能够为特定请求选择正确的上下文。使用线程本地代理是实现此目的的常用方法。
9.2章中没有提到HttpServletRequest。
所以问题是:在并发方面将 HttpServletRequest 注入自定义 ContainRequestFilter 中是否安全?
我的意思是:
@Provider
@PreMatching
public class AuthenticationFilter implements ContainerRequestFilter {
@Context private HttpServletRequest request;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// This is safe because every thread call the method with its requestContext
String path = requestContext.getUriInfo().getPath(true);
// Is this safe? The property request is injected by using @Context annotation (see above)
String toReturn = (String)request.getAttribute(name);
[...]
}
我在调试模式下对我的 IDE 进行了一些实证测试,使用两个不同的浏览器发送两个不同的并发请求,它似乎运行良好;我注意到过滤器的实例是一样的(它是一个单例),但是在这两种情况下注入的 HttpServletRequest 是不同的。
我什至读了这个帖子:How to access wicket session from Jersey-2 request filter?,看来我的测试得到了证实。
但我仍有疑问。
确认?
【问题讨论】:
标签: jersey-2.0