问题描述
我正在使用 JAX-RS/Jersey 开发 Web 服务.
I am working on a WebService using JAX-RS/Jersey.
我已经设置了一个 ContainerRequestFilter,其目的是对用户进行身份验证.我只需要通过身份验证保护一些路径,其余的每个人都可以使用.
I've set up a ContainerRequestFilter whose purpose is to authenticate the user. I only need to protect some of the paths with authentication, the rest can be available to everyone.
我想通过我的 ContainerRequestFilter 中的 ExtendedUriInfo 检索匹配的资源/匹配的结果,以便我可以检查路径是否应该受到保护.有没有办法创建一个过滤器,在填充 ExtendedUriInfo 之后,但在调用匹配的资源类和方法之前调用?
I want to retrieve matchedResources / matchedResults via ExtendedUriInfo in my ContainerRequestFilter so that I can check if the path should be protected or not. Is there a way to create a filter which is invoked after ExtendedUriInfo is populated, but before the matched resource class and method is invoked?
推荐答案
这里有一个更一般的答案(例如,如果您使用另一个 jax-rs 实现,如 CXF):
Here is a more general answer (for instance if you're using another jax-rs implementation like CXF):
只需在过滤器类中添加以下内容作为实例变量:
Just add the following in your filter class as an instance variable:
@Context
资源信息信息;
"javax.ws.rs.container.ResourceInfo 是一个新的 JAX-RS 上下文,它可以被注入过滤器和拦截器并检查哪个资源类和方法即将被调用."
"javax.ws.rs.container.ResourceInfo is a new JAX-RS context which can be injected into filters and interceptors and checked which resource class and method are about to be invoked."
(来源:https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Basics#JAX-RSBasics-ResourceInfo)
这篇关于如何在 ContainerRequestFilter 中检索请求的匹配资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!