2016-08-25 14 views
0

と私は、リソースのメソッドが、カスタム認証フィルタが呼び出されていないに私のカスタムannotaionを追加 How to Optionally Protect a Resource with Custom Dropwizard FilterDropwizard customAuthorizationFilterはDynamicFeature

下記のリンクにpandadbによって回答に与えられたすべての手順に従ってきました。

誰かが私が逃したかもしれないものを教えてもらえますか?

更新: - java8を使用してdropwizard 1.0を使用していて、mavenを使用してアプリケーションを構築しています。

+0

、それ以外の場合は、時代遅れの下に主の答えをレンダリングし、新しい質問してください。ありがとう! – halfer

答えて

1

まず、this Dropwizard Feature exampleDropwizard Authorizationを確認します。それから、あなたが何をしているのか、あなたが使っているDropwizardのバージョンについてもっと詳しく説明してください。あなたはすでにやっていることを私は推測しているすべての後


、...あなたは、カスタム認可プロバイダを作成する必要があり

public class YourCustomAuthorizer implements Authorizer<User> { 
    @Override 
    public boolean authorize(User user, String role) { 
     return user.getName().equals("good-guy") && role.equals("ADMIN"); 
    } 
} 

あなたのリソースに注釈を付けましたか?

@RolesAllowed("ADMIN") 
@GET 
public SecretPlan getSecretPlan() { 
    return dao.findPlanForUser(user); 
} 

アプリケーションの実行方法に認証クラスと認可クラスを登録しましたか?あなたの認証が前に行われ、okですされている場合は、それが動作するはずです、これを行っている場合は

@Override 
public void run(ExampleConfiguration configuration, 
       Environment environment) { 
    environment.jersey().register(new AuthDynamicFeature(
      new BasicCredentialAuthFilter.Builder<User>() 
       .setAuthenticator(new YourCustomAuthenticator()) 
       .setAuthorizer(new YourCustomAuthorizer()) 
       .setRealm("SUPER SECRET STUFF") 
       .buildAuthFilter())); 
    environment.jersey().register(RolesAllowedDynamicFeature.class); 
    //If you want to use @Auth to inject a custom Principal type into your resource 
    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class)); 
} 

。あなたはすべての認証/ autorizationなしGETS認可および認証されたユーザのみ投稿を許可したい場合は、あなたがこれを行うことができます:私は、過去に持っていた

// do not add any annotations here and all users without authentication can do this GET @RolesAllowed("ADMIN") 
// do not use '@Auth User user' in method params and do not annotate this method with '@Auth' if you want non authenticated users to do the GET 
@GET 
public SecretPlan getSecretPlan() { 
    return dao.findPlanForUser(user); 
} 

//here just authorized useras can do HTTP POSTs 
@RolesAllowed("ADMIN") 
@GET 
public SecretPlan postSecretPlan() { 
    return dao.findPlanForUser(user); 
} 

別の問題を、私はANTとIVYと私のアプリケーションを構築していないということでしたMavenと一緒に。それが間違っている場合、これはいくつかの問題を引き起こす可能性があります。あなたの問題が解決されない場合は


は、「それは、助けてください動作しない」よりも多くの情報を提供してください。*

+0

質問を更新しました。 私はjava8を使ってdropwizard 1.0を使い、mavenを使ってアプリケーションをビルドしています。 –

+0

問題が解決してまだ問題が残っている場合は、新しい質問スレッドを開いてください! – hiaclibe

0

私の他の答えの後にあなたの質問を編集した後、私は今、新しい答えを投稿します。それが言うよう

読むDropwizard Documentation、その:

現在@UnitOfWorkアノテーションで取引を作成するには、唯一のジャージーによって管理されるリソースのためのアウトオブボックスに動作します。 Jerseyのリソースの外で使用する場合は、オーセンティケータでは、UnitOfWorkAwareProxyFactoryでクラスをインスタンス化する必要があります。最新の編集に関しては

SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory()); 
ExampleAuthenticator exampleAuthenticator = new UnitOfWorkAwareProxyFactory(hibernateBundle) 
       .create(ExampleAuthenticator.class, SessionDao.class, dao); 
+0

私はこれをしました。私はまだエラーが発生しています。 –

+0

ユーザーのためにDBを読んだだけの場合、なぜUnit ofWorkを使用するのですか、またはユーザー権限検索のトランザクション要求が必要なのはなぜですか?これは私にとって意味をなさないか、ユーザーが一度許可された後にユーザーの役割を変更していますか? – hiaclibe

+0

dropwizard ver 9.0以降では、認証と認証のためにdbにアクセスするにはUnitOfWorkAwareProxyFactoryでクラスを登録し、dbにアクセスしたいリソースクラスにUnitOfWorkに注釈を付ける必要があります。 –