user1578872
user1578872

Reputation: 9028

Spring RestEasy interceptor

I have a Spring boot application with resteasy-spring-3.0.19 and jboss-jaxrs-api_2.0_spec-1.0.0.

I would like to intercept all the rest calls for authorization.

The interceptor is not getting invoked. Also, How can i get the target method @Path annotation value in the interceptor.

Do I need to register this in the Spring boot app?

@Provider
public class AuthorizationtInterceptor implements ContainerRequestFilter{

    /**
     * 
     */
    public AuthorizationtInterceptor() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        String method = requestContext.getMethod();

        UriInfo uriInfo = requestContext.getUriInfo();

        // Need the target method @Path annotation value ....

    }

}

Target Rest class,

@Named
@Singleton
@Path(ROOT_PATH)
public class WebController {

    @GET
    @Path(TEST_PATH)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUser(@Context final HttpServletRequest request) {
    }
}

Upvotes: 1

Views: 967

Answers (1)

cassiomolin
cassiomolin

Reputation: 130887

In a post-matching filter (a filter without the @PreMatching annotation), you can use ResourceInfo to get the matched resource class and the resource method.

Inject ResourceInfo in your filter using the @Context annotation:

@Context
private ResourceInfo resourceInfo;

Then get the resource class and extract the @Path annotation:

Path path = resourceInfo.getResourceClass().getAnnotation(Path.class);

To get the resource method, use:

Path path = resourceInfo.getResourceMethod().getAnnotation(Path.class);

Depending on what you are trying to achieve, instead of comparing the value of the @Path annotation for authorization purposes, you could consider binding your filter to a set of resource classes or methods. See more details in this answer.


Depending on how you are setting up your application, you may need to register the filter in your Application subclass or in your web.xml deployment descriptor.

Upvotes: 2

Related Questions