0

このタスクをコンテナーに委任するのではなく、サーブレット アプリケーション内で HTTP 認証ロジックを実行する必要があります。

具体的には、HttpServletRequestHTTP 認証ヘッダーを含む のヘッダーを取得し、提供された資格情報を表すデータ構造にデコードして、アプリケーションが処理できるようにする方法が必要です。基本認証とダイジェスト認証の両方をサポートする必要があります。

私はこれを手で書くことができました.それは雑用ではありません.RFCはすべて十分に文書化されています.

私が最初に考えたのは Spring Security でしたが、これは、このタスクをコンテナーに委譲していることがわかります (それについては少しわかりませんが、これは複雑なコード ベースです)。

他に知ってる人いますか?

4

3 に答える 3

2
  • BASIC の場合、実装は非常に簡単です。ヘッダーを読み取り、base64 でデコードして、「:」文字で分割するだけです。Spring のBasicProcessingFilterを使用して、 AuthenticationManagerのインスタンスを提供することもできます。
  • ダイジェストでは、リクエストからパスワードを取得することはできません (それが要点です...)。プロトコルが十分に文書化されていても、すべての詳細を実装することは簡単な作業ではありません。したがって、Spring のDigestProcessingFilterを使用します。この場合、(ダイジェスト用に) ユーザー名に基づいてユーザーのパスワードを提供するUserDetailsS​​erviceを提供する必要があります。
于 2009-10-19T16:20:59.620 に答える
1

これが私のデコーダです:

public static String[] decodeBasicAuth(String authorization) {
    if (authorization == null)
        throw new RuntimeException("Invalid Authorization String.");
    if (authorization.length() < 9)
        throw new RuntimeException("Invalid Authorization String.");
    if (authorization.length() > 64)
        throw new RuntimeException("Invalid Authorization String.");
    String s[] = authorization.split("\\s", 3);
    if (s.length < 2)
        throw new RuntimeException("Invalid Authorization String.");
    for (int i = 0; i < s.length; i++) {
        String part = s[i];
        if (part.compareTo("Basic") == 0) {
            String userPassBase64 = s[i + 1];
            if (!userPassBase64.isEmpty()) {
                String userPass = null;
                try {
                    userPass = new String(DatatypeConverter.parseBase64Binary(userPassBase64));
                } catch (RuntimeException e) {
                    throw new RuntimeException("Authorization cannot be decoded.", e);
                }
                String userPassArray[] = userPass.split(":");
                if (userPassArray.length == 2) {
                    return userPassArray;
                } else {
                    throw new RuntimeException("Invalid Authorization String.");
                }
            } else {
                throw new RuntimeException("Invalid Authorization String.");
            }
        }
    }
    throw new RuntimeException("Authorization cannot be decoded.");
}
于 2012-07-30T23:25:01.220 に答える
0

フレームワークについてはわかりませんが、BASIC 認証を使用していない限り、ユーザーのパスワードを取得できない可能性があります。

Base64DecodeBASIC 認証を使用している場合、Authenticationヘッダーに関しては非常に簡単です。

于 2009-10-19T15:45:10.927 に答える