What is the proper replacement of the Resteasy 3.X PreProcessInterceptor?

后端 未结 3 1475
孤城傲影
孤城傲影 2020-12-15 03:58

I\'m building rest service using an authentication/authorization mechanism as described in this tutorial: http://howtodoinjava.com/2013/06/26/jax-rs-resteasy-basic-authentic

相关标签:
3条回答
  • 2020-12-15 04:19

    I also wanted to get access to the underlying java.lang.reflect.Method and tried mtpettyp's answer with Resteasy 3.0.8, but that was returning null on the getProperty call. I am also using Spring and resteasy-spring although I don't believe that should impact this at all.

    If you run into my situation and are implementing a Post Matching ContainerRequestFilter (you kind of have to if you were expecting to get the matched resource method anyway) then you can actually cast the ContainerRequestContext to the implementation Resteasy has for the Post Match scenario. The PostMatchContainerRequestContext has a reference to the ResourceMethodInvoker.

    public void filter(ContainerRequestContext context) throws IOException {
        PostMatchContainerRequestContext pmContext = (PostMatchContainerRequestContext) context;
    
        Method method = pmContext.getResourceMethod().getMethod();
    
        /* rest of code here */
    }
    
    0 讨论(0)
  • 2020-12-15 04:34

    RESTEasy 3.x.x conforms to the JAX-RS 2.0 specification.

    What you are trying to do could be accomplished (maybe better) with:

    @Provider
    public class SecurityInterceptor 
          implements javax.ws.rs.container.ContainerRequestFilter {
         @Override
         public void filter(ContainerRequestContext requestContext){
           if (not_authenticated){ requestContext.abortWith(response)};
         }
    }
    

    since the ReaderInterceptor is invoked only if the underlying MessageBodyReader.readFrom is called by the standard JAX-RS pipeline, not fromthe application code.

    The reason why your interceptor is not called, though, could be the @ServerInterceptor annotation, which is a RESTEasy extension.

    The spec states at §6.5.2 that a interceptor is globally registered, unless the @Provider is annotated with a @NameBinding annotation, but I don't know if RESTEasy can handle a @ServerInterceptor if it's not explicitly registered as shown in RestEASY Interceptor Not Being Called

    0 讨论(0)
  • 2020-12-15 04:38

    If you need to get access to the underlying java.lang.reflect.Method (like you used to be able to get by implementing AcceptedByMethod), you can do the following:

    ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) 
                requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
    Method method = methodInvoker.getMethod();
    
    0 讨论(0)
提交回复
热议问题