1

I'm trying to figure out how to inject an object when registering a AuthDynamicFilter in jersey.

public class CustomApplication extends Application<Configuration> {
   public void run(Configuration configuration, Environment environment) throws Exception {
      ...

      environment.jersey().register(new AuthDynamicFeature(CustomAuthFilter.class));   
      ...
   }
}

CustomAuthFilter

@PreMatching
public class CustomAuthFilter<P extends Principal> extends AuthFilter<String, P> {
   private final Object identity;

   @Context
   private HttpServletRequest httpServletRequest;

   @Context
   private HttpServletResponse httpServletResponse;

   public LcaAuthFilter(Identity identity) {
     this.identity = identity;
   }

   @Override
   public void filter(ContainerRequestContext requestContext) throws IOException {
       ...identity.process(httpServletRequest)
   }

}

The above code won't compile because there is no way to inject the Identity Object when creating the CustomAuthFilter.

If you go with:

   environment.jersey().register(new AuthDynamicFeature(new CustomerAuthFilter(new Identity())));

In this case, httpServletRequest will be set to null.

The only way I could figure out how to get around this is to not even use AuthDynamicFeature and just go with a normal Filter and inject it that way which is fine. I'm wondering how would you do it with a AuthDynamicFeature.

I'm still new to dropwizard and jersey so please bear with me. Some concepts that I might be messing.

Any Advice Appreciated, Thanks, Derek

darewreck
  • 2,576
  • 5
  • 42
  • 67

1 Answers1

0

I have an app that does exactly that. Annotate the constructor with @Inject:

@PreMatching
public class CustomAuthFilter<P extends Principal> extends AuthFilter<String, P> {
   private final Identity identity;

   @Context
   private HttpServletRequest httpServletRequest;

   @Context
   private HttpServletResponse httpServletResponse;

   // Annotate the ctor with @Inject
   @Inject
   public LcaAuthFilter(Identity identity) {
     this.identity = identity;
   }

   @Override
   public void filter(ContainerRequestContext requestContext) throws IOException {
       ...identity.process(httpServletRequest)
   }

}

Tell Jersey's dependency injection mechanism to inject the identity with a value you provide:

Identity identity = getTheIdentity();
environment.jersey().register(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(identity).to(Identity.class);
    }
});
environment.jersey().register(new AuthDynamicFeature(CustomAuthFilter.class));

If identity is only known at runtime, use a Supplier<Identity> instead (adapt your constructor as well):

Supplier<Identity> identity = getTheIdentitySupplier();
environment.jersey().register(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(identity).to(new TypeLiteral<Supplier<Identity>>(){});
    }
});
environment.jersey().register(new AuthDynamicFeature(CustomAuthFilter.class));
andref
  • 4,460
  • 32
  • 45