我需要在春季进行自定义身份验证,它应该是一个简单的类,它接受用户提供的用户名和密码,并将其与某些值进行比较,并根据它进行身份验证。
我们使用GWT制作的系统,我们的登录表单,成功后,在新窗口中打开另一页。
这是我到目前为止所尝试的: file:application-context安全性:
...
<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();
问题:
答案 0 :(得分:1)
听起来像你需要编写自己的UserDetailsService。这是Spring Security接口,它使用UserDetails填充SecurityContext.getPrincipal()。如果在安全过滤器链的末尾没有填充此对象,那么Spring将抛出一个可以捕获的授权异常,并且可以将用户重定向到应用程序的另一个页面/部分。