title: HK2 MethodInterceptor with Jersey resource
body: <p>How can I set up an aop <code>MethodInterceptor</code> to work with Jersey resources?</p>
  
  <p>Here is what I've tried, following <a href="https://hk2.java.net/2.2.0/aop-example.html" rel="nofollow noreferrer">this</a> documentation:</p>
  
  <p><strong>Step 1 - InterceptionService</strong></p>
  
  <pre><code>public class MyInterceptionService implements InterceptionService
  {
      private final Provider&lt;AuthFilter&gt; authFilterProvider;
  
      @Inject
      public HK2MethodInterceptionService(Provider&lt;AuthFilter&gt; authFilterProvider)
      {
          this.authFilterProvider = authFilterProvider;
      }
  
      /**
       * Match any class.
       */
      @Override
      public Filter getDescriptorFilter()
      {
          return BuilderHelper.allFilter();
      }
  
      /**
       * Intercept all Jersey resource methods for security.
       */
      @Override
      @Nullable
      public List&lt;MethodInterceptor&gt; getMethodInterceptors(final Method method)
      {
          // don't intercept methods with PermitAll
          if (method.isAnnotationPresent(PermitAll.class))
          {
              return null;
          }
  
          return Collections.singletonList(new MethodInterceptor()
          {
              @Override
              public Object invoke(MethodInvocation methodInvocation) throws Throwable
              {
                  if (!authFilterProvider.get().isAllowed(method))
                  {
                      throw new ForbiddenException();
                  }
  
                  return methodInvocation.proceed();
              }
          });
      }
  
      /**
       * No constructor interception.
       */
      @Override
      @Nullable
      public List&lt;ConstructorInterceptor&gt; getConstructorInterceptors(Constructor&lt;?&gt; constructor)
      {
          return null;
      }
  }
  </code></pre>
  
  <p><strong>Step 2 - Register the service</strong></p>
  
  <pre><code>public class MyResourceConfig extends ResourceConfig
  {
      public MyResourceConfig()
      {
          packages("package.with.my.resources");
  
          // UPDATE: answer is remove this line
          register(MyInterceptionService.class);
  
          register(new AbstractBinder()
          {
              @Override
              protected void configure()
              {
                  bind(AuthFilter.class).to(AuthFilter.class).in(Singleton.class);
  
                  // UPDATE: answer is add the following line
                  // bind(MyInterceptionService.class).to(InterceptionService.class).in(Singleton.class);
              }
          });
      }
  }
  </code></pre>
  
  <p>However this doesn't appear to work because none of my resource methods are being intercepted. Could this be because I use <code>@ManagedAsync</code> with all of my resources? Any ideas?</p>
  
  <p>Also, please do not suggest a <code>ContainerRequestFilter</code>. See <a href="https://stackoverflow.com/q/20556200/1415732">this question</a> for why I can't use one to handle security. </p>