【问题标题】:I need a simple custom authentication in spring我在春天需要一个简单的自定义身份验证
【发布时间】:2010-12-09 01:20:24
【问题描述】:

我需要在 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. 我不想硬编码 authentication-provider 中的值,还有其他方法可以让我提供自己的类吗?我尝试了几个教程但徒劳无功,它们似乎都指向我允许 spring 通过数据库或其他类型的文件进行比较工作,我不能自己做吗?

【问题讨论】:

标签: gwt web spring-security


【解决方案1】:

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

【讨论】:

  • 我现在真的很沮丧,我已经尝试了 1000 件事情(字面意思)做到了??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多