I am working on a WebService using JAX-RS/Jersey.
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.
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?
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
ResourceInfo info;
"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."
(source : https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Basics#JAX-RSBasics-ResourceInfo)
Original answer here
Found a way to do it with ContainerRequestFilter:
public void filter(ContainerRequestContext requestContext) {
UriRoutingContext routingContext = (UriRoutingContext) requestContext.getUriInfo();
ResourceMethodInvoker invoker = (ResourceMethodInvoker) routingContext.getInflector();
Class<?> className = invoker.getResourceClass();
Method methodName = invoker.getResourceMethod();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With