2016-04-05 12 views
0

Spring Securityでの私の問題についてお手伝いをしたいと思います。 私は、ユーザーが選択したオプションに基づいてログイン資格情報を検証する必要があります。オプション1は、第三者サービスを介してログインしているユーザーを検証します。オプション2は、データベース認証レベルを使用した通常の検証です。これをどのように実装できますか?Spring Securityでのカスタム認証の実装

答えて

5

一般的な戦略は

  1. org.springframework.security.authentication.AuthenticationProviderのカスタム実装を提供することを委任し、適切なバックエンドに認証(サードパーティのサービスを、別のAuthenticationProvider、など)。
  2. ユーザーが選択したオプションをカスタムAuthenticationProviderに渡し、正しい認証バックエンドを選択できるようにします。
  3. デフォルトの認証プロバイダとしてカスタムAuthenticationProviderを設定します。

ステップ1:AuthenticationProvider

AuthenticationProvider実装は、単一のメソッドとのインタフェースです。

class DelegatingAuthenticationProvider implements AuthenticationProvider { 
    @Autowired 
    private ThirdPartyAuthenticationService service; 

    @Autowired 
    @Qualifier("anotherAuthenticationProvider") 
    private AuthenticationProvider provider; 

    @Override 
    public Authentication authenticate(final Authentication authentication) throws AuthenticationException { 
    // Get the user selection. 
    String selection = (String) authentication.getDetails(); 

    // Take action depending on the selection. 
    Authentication result; 
    if("ThirdParty".equals(selection)) { 
     // Authenticate using "service" and generate a new 
     // Authentication "result" appropriately. 
    } 
    else { 
     // Authenticate using "provider" and generate a new 
     // Authentication "result" appropriately. 
    } 

    return result; 
    } 
} 

ステップ2:そのため、カスタム実装は次のように見えるかもしれAuthenticationProvider実装は上記のdetailsプロパティからユーザーの選択をピックアップAuthenticationProvider

にユーザ選択を渡しますAuthenticationオブジェクトです。おそらく、ユーザの選択はHttpServletRequestから取得し、AuthenticationProviderを呼び出す前にAuthenticationオブジェクトに追加する必要があります。これは、AuthenticationHttpServletRequestオブジェクトの両方にアクセスできる別のコンポーネントで、AuthenticationProviderを実装する必要がある前に呼び出されます。

Authenticationオブジェクトは、AbstractAuthenticationProcessingFilterの実装によって作成されます。このクラスには、のオブジェクトを受け入れ、というメソッドがあり、Authenticationオブジェクトを返します。だから、これは必要なものを実装するための良い候補になると思われる。ユーザ名とパスワードベースの認証の場合、実装クラスはUsernamePasswordAuthenticationFilterです。このクラスはという新しいインスタンスを返します。これはAuthenticationの実装です。だから、UsernamePasswordAuthenticationFilterのクラスで十分です。

class ExtendedUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter { 
    @Override 
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { 
    ... 
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password); 
    authentication.setDetails(obtainUserSelection(request)); 

    ... 

    return authentication; 
    } 
} 

obtainUserSelectionは、リクエストからユーザー選択を取得するプライベートメソッドです。

ステップ3:設定

は春のセキュリティ構成のAuthenticationProviderとフィルタの実装を設定します。正確な手順は、XMLまたはJava構成のどちらを使用するかによって異なります。

+0

wow..thanks manish!これは、春のセキュリティを学ぶ乗り物の1つの地獄です。これは私の最初の実装です。 ExtendedUsernamePasswordAuthenticationFilterの実装に関するチュートリアルサイトを教えてください。 please: –

+0

[UsernamePasswordAuthenticationFilter'の公式の実装](https://github.com/spring-projects/spring-security/blob/master/web/src/main/java/org/springframework/security)を確認することができます。 /web/authentication/UsernamePasswordAuthenticationFilter.java)。 – manish

関連する問題