本文介绍了如何在ContainerRequestFilter中检索请求的匹配资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS/Jersey开发WebService.

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检索matchedResources/matchedResults,以便我可以检查路径是否应受到保护.是否有一种方法可以创建一个过滤器,该过滤器在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?

推荐答案

这是一个更通用的答案(例如,如果您使用的是像CXF这样的其他jax-rs实现):

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:

ResourceInfo信息;

ResourceInfo info;

"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中检索请求的匹配资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:00