2017-04-15 47 views
1

我在我的代码中尝试使用Dropwizard身份验证,但在运行时遇到POST调用时出现的一些问题,尽管它在GET中正常工作。这是我如何在GET调用使用此:使用POST调用失败的Dropwizard身份验证

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

然后在邮政呼叫时不工作:

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

UPDATE

下面是我已经注册了我的LDAP认证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)); 

回答

1

最可能的原因是,你还没有配置AUTH功能正常。大多数人忘记的一件事是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)); 

Basic Authentication docs

参见:

  • My comment为Dropwizard问题就同样的问题。你会很好地解释导致问题的原因。
+0

尼斯解释,现在我知道资源是如何初始化,但问题是,我已经注册AuthValueFactoryProvider.Binder在我的代码,仍然发生此问题,请检查问题 – user2098324

+0

我更新这是一个运行时(根据请求)错误或启动错误?如果前者,我认为我的假设是错误的。如果这是一个启动错误,它将是一个ModelValidationException。但它看起来像错误发生在请求上,因为它找不到要注入的对象。在这种情况下,确保你实际上从认证者返回一个用户。这是我能想到的唯一想法是,在运行时会出现这个错误 –

+0

它实际上是一个启动错误,但在堆栈跟踪中没有ModelValidationException,请检查日志https://justpaste.it/15luy – user2098324