Authentication Dropwizard身份验证与POST调用失败

Authentication Dropwizard身份验证与POST调用失败,authentication,jersey,dropwizard,Authentication,Jersey,Dropwizard,我在代码中尝试退出Dropwizard身份验证,但在运行时的POST调用中遇到了一些问题,尽管GET可以正常工作。以下是我在GET调用中使用此选项的方式: @Override @GET @Path("/auth") public Response doAuth(@Auth User user) { //do something } 然后在不起作用的Post通话中: @Override @POST @Path("/") public Response createLegalEntity(

我在代码中尝试退出Dropwizard身份验证,但在运行时的POST调用中遇到了一些问题,尽管GET可以正常工作。以下是我在GET调用中使用此选项的方式:

@Override
@GET
@Path("/auth")
public Response doAuth(@Auth User user) {
    //do something
}
然后在不起作用的Post通话中:

@Override
@POST
@Path("/")
public Response createLegalEntity(@Auth User user, LegalEntity createLegalEntity) {
    // do something
}
运行时,它抛出以下错误:

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
我是Dropwizard的新手,无法找出问题的原因

更新

以下是我注册ldap身份验证配置的方式:

 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));
final LdapConfiguration LdapConfiguration=configuration.getLdapConfiguration();
Authenticator ldapAuthenticator=新的CachingAuthenticator(
environment.metrics(),
新的ResourceAuthenticator(新的LdapAuthenticator(ldapConfiguration)),
ldapConfiguration.getCachePolicy());
environment.jersey().register(新AuthDynamicFeature)(
新建BasicCredentialAuthFilter.Builder()
.setAuthenticator(ldapAuthenticator)
.setRealm(“LDAP”)
.buildAuthFilter());
register(新的AuthValueFactoryProvider.Binder(User.class));

最可能的原因是您没有正确配置身份验证功能。大多数人忘记的一件事是
AuthValueFactoryProvider.Binder
。还需要注册此类的实例。如果未注册,这肯定会导致您看到的错误

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

environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
//如果要使用@Auth将自定义主体类型注入到资源中
register(新的AuthValueFactoryProvider.Binder(User.class));

另请参见:

  • 有关同一问题的Dropwizard问题。您将很好地解释问题的原因

很好的解释,现在我知道了资源是如何初始化的,但问题是,我已经注册了AuthValueFactoryProvider.Binder。在我的代码中,这个问题仍然存在,请检查我的更新问题这是运行时(应请求)错误还是启动错误?如果是前者,我认为我的假设是错误的。如果是启动错误,则是ModelValidationException。但是,由于找不到要注入的对象,错误似乎是在请求时发生的。在这种情况下,请确保您实际上是从验证器返回用户。这是我能想到的唯一一个在运行时会发生的错误。这实际上是一个启动错误,但是stacktrace中没有ModelValidationException,请检查日志,查看stacktrace,看起来您使用的是Jersey 1.x,它是Dropwizard=0.8.x,它是Jersey 2.x。这些版本不兼容。您可以签出0.7配置。如果这是一个新的项目,我建议您切换到最新的Dw版本,即1.1.0。见