2017-04-15 42 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