0

我需要在 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();

问题:

  1. 登录不正确:它显示成功并打开包含登录屏幕的弹出窗口!(显然登录没有成功,但登录屏幕无法检测到)
  2. 我不想对身份验证提供程序中的值进行硬编码,是否有另一种方法可以让我提供自己的类?我尝试了几个教程但徒劳无功,它们似乎都指向我允许 spring 通过数据库或其他类型的文件进行比较工作,我不能自己做吗?
4

1 回答 1

1

在我看来,您需要编写自己的 UserDetailsS​​ervice。这是使用 UserDetails 填充 SecurityContext.getPrincipal() 的 Spring Security 接口。如果在安全过滤器链结束时这个对象还没有被填充,那么 Spring 将抛出一个可以被捕获的授权异常,并且用户可以被重定向到应用程序的另一个页面/部分。

于 2009-10-22T15:59:07.113 回答