user2098324
user2098324

Reputation: 972

Dropwizard Authentication with POST calls failing

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));

Upvotes: 1

Views: 278

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209052

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.

Upvotes: 1

Related Questions