我需要在 spring 中进行自定义身份验证,它应该是一个简单的类,它接受用户提供的用户名和密码并将其与一些值进行比较,并以此为基础进行身份验证。
我们正在使用 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>
...
自定义登录代码(单击确定按钮):
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 通过数据库或其他类型的文件进行比较工作,我不能自己做吗?