1

I was trying out Dropwizard authentication in my code but facing some issue in POST call at runtime, although its working fine with GET. this is how I am using this in the GET call:

@Override
@GET
@Path("/auth")
public Response doAuth(@Auth User user) {
    //do something
}

And then in Post call which is not working:

@Override
@POST
@Path("/")
public Response createLegalEntity(@Auth User user, LegalEntity createLegalEntity) {
    // do something
}

While running it is throwing following error:

SEVERE: Missing dependency for method public javax.ws.rs.core.Response org.flipkart.api.LegalEntityResource.createLegalEntity(com.yammer.dropwizard.authenticator.User,org.flipkart.model.LegalEntity) at parameter at index 0

I am new to Dropwizard and not able to figure out the cause of the problem.

UPDATE

Here is how I have registered my ldap authentication configs:

 final LdapConfiguration ldapConfiguration = configuration.getLdapConfiguration();
    Authenticator<BasicCredentials, User> ldapAuthenticator = new CachingAuthenticator<>(
            environment.metrics(),
            new ResourceAuthenticator(new LdapAuthenticator(ldapConfiguration)),
            ldapConfiguration.getCachePolicy());

    environment.jersey().register(new AuthDynamicFeature(
            new BasicCredentialAuthFilter.Builder<User>()
                    .setAuthenticator(ldapAuthenticator)
                    .setRealm("LDAP")
                    .buildAuthFilter()));

    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
user2098324
  • 972
  • 10
  • 17

1 Answers1

1

The most likely reason is that you have not configured that auth feature correctly. The one thing that most people forget about is the AuthValueFactoryProvider.Binder. An instance of this class also needs to be registed. This would definitely cause the error you are seeing, if unregistered.

// If you want to use @Auth to inject a custom Principal type into your resource

environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));

From Basic Authentication docs

See also:

  • My comment for Dropwizard issue regarding the same problem. You will get a good explanation of the what causes the problem.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Nice explanation, now I know how resources are initialised, but the problem is, I have already registered AuthValueFactoryProvider.Binder in my code, still this problem is occurring, Please check my update on the question – user2098324 Apr 17 '17 at 06:34
  • Is this a runtime (on request) error or startup error? If the former, I think I was wrong in my assumption. If it was a startup error, it would be a ModelValidationException. But it looks like the error is happening on request, as it can't find an object to inject. In which case, make sure you're actually returning a User from the authenticator. That's the only think I can think of that would can this error at runtime – Paul Samsotha Apr 17 '17 at 06:46
  • it is actually a start up error, but there is no ModelValidationException in the stacktrace, please check the logs https://justpaste.it/15luy – user2098324 Apr 17 '17 at 08:07
  • So looking at the stacktrace, it looks like you're using Jersey 1.x, which is Dropwizard <= 0.7.x. The auth configuration you have is for Dw >= 0.8.x, which is Jersey 2.x These version are incompatible. You can check out [this post](http://stackoverflow.com/questions/27392224/dropwizard-auth-by-example/27393546#27393546) for the 0.7 configuration. It's really only a one-liner – Paul Samsotha Apr 17 '17 at 08:15
  • If it's a new project you're working on, I would suggest you switch to the newest Dw version, which is 1.1.0. See the [getting started](http://www.dropwizard.io/1.1.0/docs/getting-started.html) – Paul Samsotha Apr 17 '17 at 08:18