1

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) {
    }
}
user1578872
  • 7,808
  • 29
  • 108
  • 206

1 Answers1

2

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.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • It is getting invoked after registering the filter as if we register other filters in class with @Configuration annotation. Path path = resourceInfo.getResourceClass().getAnnotation(Path.class); returns class annotation, not the method that matches. I need the HttpServletRequest also, So I use Context private HttpServletRequest request;. Is this ok? – user1578872 Oct 17 '16 at 14:33
  • 1
    @user1578872 Yes, you can inject the `HttpServletRequest` using `@Context HttpServletRequest request`. For a complete list of types that can be injected with `@Context`, refer to this [answer](http://stackoverflow.com/a/35868654/1426227). – cassiomolin Oct 17 '16 at 14:37