我需要在春天进行简单的自定义身份验证

时间:2009-10-22 11:11:45

标签: gwt web spring-security

我需要在春季进行自定义身份验证,它应该是一个简单的类,它接受用户提供的用户名和密码,并将其与某些值进行比较,并根据它进行身份验证。

我们使用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();

问题:

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

1 个答案:

答案 0 :(得分:1)

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