10

環境

認証されたユーザーが、組織によって公開された API で使用するOAuth2 ベアラー トークンを作成できるようにするアプリケーションを開発しています。アイデアは、GitHub のPersonal API tokensと同様に、ユーザーがそのようなトークンを自己生成/取り消しできるようにすることです。その後、ユーザーは発行されたトークンを使用して、保護されたリソースへのプログラムによるアクセスを取得できます。この構成では、OAuth の「クライアント」、「認可サーバー」、「リソース サーバー」が組織に属します。今のところ、これらのサービスはすべて同じプロセスにあります。

この目的のために、私はResource Owner Password Credentials Grantタイプをサポートしようとしています。実装環境は以下で構成されます。

  • スプリングブーツ
  • Spring セキュリティ OAuth2

実装の制約の 1 つは、保存されているパスワードにアクセスできないことです。この処理は、実際の認証を行う内部 Web サービスに委任されます。

問題

保存されたパスワードにアクセスできないという制約があるため、構成されたデフォルトは使用できません。これは、プロバイダーの によって返されるオブジェクトDaoAuthenticationProviderによって提供されるパスワードにアクセスする必要があるためです。UserDetailsUserDetailsService

AuthenticationProviderこれをカスタム実装に置き換える必要があると思います。ただし、そうしようとするすべての試みが効果を発揮するわけではないようです。

parent以下は のリファレンスに正しく登録されているように見えますAuthenticationManagerが、実行時に委任されません (DaoAuthenticationProvider優先されるため):

@Configuration
public class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(new AuthenticationProvider() {

      @Override
      public boolean supports(Class<?> authentication) {
        // For testing this is easier, but should check for UsernamePasswordAuthentication.class
        return true;
      }

      @Override
      public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // Perform custom logic...
        return authentication;
      }

    });
  }

}

私が何をしようとしても(以下の参考文献を参照してください)、私は常に次の2つのプロバイダをメソッドで呼び出すProviderManagerときに取得します:BasicAuthenticationFilterAuthentication authResult = authenticationManager.authenticate(authRequest)doFilter

[ org.springframework.security.authentication.AnonymousAuthenticationProvider@366815e4, org.springframework.security.authentication.dao.DaoAuthenticationProvider@5da3e311 ]

AuthorizationServerSecurityConfigurerこれは'sclientCredentialsTokenEndpointFilterメソッドの結果である可能性があると思います。ただし、このクラスはマークされfinalているため、カスタマイズできません。

提案や指針をいただければ幸いです。

資力

私が試した/研究したこと:

4

2 に答える 2

1

コメントで述べたように、次のスニペットで「グローバル」AuthenticationManager を自動配線するには、何らかの形でコンテキストに認識させる必要があります。

スニペット:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
   endpoints.authenticationManager(authenticationManager);
}

HttpSecurity の特定のインスタンスのみを構成します。

個人的には、Bean をプライマリとして設定し、中央構成の順序を 1 に設定して、この構成が他の (自動) 構成よりも優先されるようにすることを好みます。

@Order(1)
@Configuration
public class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {

    @Bean
    @Primary
    public AuthenticationProvider authenticationProvider() {
        return new AuthenticationProvider() {

            @Override
            public boolean supports(Class<?> authentication) {
                // For testing this is easier, but should check for
                // UsernamePasswordAuthentication.class
                return true;
            }

            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {
                // Perform custom logic...
                return authentication;
            }

        };
    }
}
于 2018-04-01T04:27:18.640 に答える