質問
春にカスタム認証が必要です。これは、ユーザーから提供されたユーザー名とパスワードを受け取り、それをいくつかの値と比較し、それに基づいて認証する単純なクラスである必要があります。
私たちは GWT で作られたシステムを使用しており、ログインフォームが成功すると、新しいウィンドウで別のページが開きます。
これは私がこれまでに試みたことです:ファイル:アプリケーションコンテキストのセキュリティ:
...
<http auto-config="true">
<intercept-url pattern='/*Login.html' access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern='/**/*MainScreen.html' access="ROLE_ADMIN"/>
<form-login login-page="/Login.html"/><!-- This is the default login page -->
</http>
<authentication-provider>
<user-service>
<user name="test1" password="abc" authorities="ROLE_ADMIN" />
<user name="test2" password="test" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
...
カスタム ログイン コード ([OK] ボタンをクリックしたとき):
RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST,"j_spring_security_check");
requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");
//-- sending the username and the password in designated fields.
//Hard coding values for testing reasons:
requestBuilder.setRequestData("j_username=test1" +
"&j_password=abc");
requestBuilder.setCallback(new RequestCallback() {
public void onError(Request request, Throwable exception)
{
Window.alert("ERROR !!!"+exception.getMessage());
}
public void onResponseReceived(Request request, Response response)
{
if (response.getStatusCode() != Response.SC_UNAUTHORIZED && response.getStatusCode() != Response.SC_OK)
{
onError(request, new RequestException(response.getStatusText() + ":\n" + response.getText()));
return;
}
if (response.getStatusCode() == Response.SC_UNAUTHORIZED)
{
//This code is never encountered !! :((
Window.alert("You have entered an incorrect username or password. Please try again.");
}
else
{
String height = 800+"";
String width = 600+"";
Window.alert("Authorisation succeeded, you may enter....");
Window.open("MainScreen.html", "Main screen!!", "height=" + height + ",width=" + width
+ ",scrollbars=yes,resizable=yes,titlebar=no,toolbar=no,status=yes,close=no,left=0,top=0");
}
}
});
requestBuilder.send();
問題点:
- 不正なログイン:成功したことが表示され、ログイン画面を含むポップアップが開きます。(明らかにログインは成功しませんでしたが、ログイン画面はそれを検出できませんでした)
- 認証プロバイダーの値をハードコーディングしたくないのですが、独自のクラスを提供するような別の方法はありますか?いくつかのチュートリアルを試しましたが、どれも、Spring がデータベースまたはその他の種類のファイルを介して比較作業を実行できるようにすることを示しているようです。自分で行うことはできませんか?
解決
独自の UserDetailsService を作成する必要があるように思えます。これは、SecurityContext.getPrincipal() に UserDetails を設定する Spring Security インターフェイスです。セキュリティ フィルタ チェーンの終わりまでにこのオブジェクトが設定されていない場合、Spring は承認例外をスローします。これをキャッチして、ユーザーをアプリケーションの別のページ/部分にリダイレクトできます。
所属していません StackOverflow