1

このサイトに触発されたremembermecookieに基づいてページでユーザーを認証する必要があります:春の認証をチェックするためのチュートリアル、認証をチェックするためのソリューションを思いつきました。

アプリケーションに加えられた変更

applicationContext-security.xml:

<intercept-url pattern='/**AuthenticationChecker.html' access="ROLE_ADMIN"/>
...
<form-login login-page="/Login.html" authentication-failure-url="/Login.html" always-use-default-target="true" default-target-url="/Main.html"/>

Gwtコード:

try
{
    RequestBuilder rb = new RequestBuilder(
    RequestBuilder.POST, "AuthenticationChecker.html");
            rb.sendRequest(null, new RequestCallback() 
            {
                public void onError(Request request, Throwable exception)
                {
                    RootPanel.get().add(new HTML("[error]" + exception.getMessage()));
                }
                public void onResponseReceived(Request request, Response response)
                {
                    RootPanel.get()
                   .add(new HTML("[success (" + response.getStatusCode() + "," + response.getStatusText() + ")]"));
                }
            }
    );
}
catch (Exception e)
{
    RootPanel.get().add(new HTML("Error sending request " + e.getMessage()));
}

AuthenticationChecker.htmlは単純な空白のhtmlページであり、AuthenticationChecker.htmlには管理者としての役割が必要であるため、Cookieが存在しなかった場合は、401 Unauthorizedを取得し、ユーザーが認証された場合は200OKを取得する必要があります。存在していました。

ただし、出力には常に次のように表示されます。[success(200、OK)]

クロスチェックするには、(ログインせずに)authenticaionChecker.htmlと入力するだけで、Login.htmlに戻り、Springが実際にユーザーを認証していることを示します。

私はここで何か間違ったことをしていますか?

4

1 に答える 1

2

チュートリアルを見ると、基本認証を使用している場合にのみ 401 が返されることがわかります。フォームベースの認証では、エラー メッセージの応答テキストを確認する必要があります。例えば:

public void onResponseReceived(Request request, Response response) {
    if (response.getStatusCode() != Response.SC_OK) {
        onError(request, new RequestException(response.getStatusText() + ":\n" + response.getText()));
        return;
    }

    if (response.getText().contains("Access Denied")) {
        Window.alert("You have entered an incorrect username or password. Please try again.");
    } else {
        // authentication worked, show a fancy dashboard screen
    }
}
于 2009-11-25T09:04:01.780 に答える