views:

764

answers:

1

I need to authenticate a user in a page based on the remember me cookie, inspired by this site: Tutorial for checking spring authentication, I came up with a solution for checking the authentication.

Changes made in my application

applicationContext-security.xml:

<intercept-url pattern='/**AuthenticationChecker.html' access="ROLE_ADMIN"/>
...
<form-login login-page="/Login.html" authentication-failure-url="/Login.html" always-use-default-target="true" default-target-url="/Main.html"/>

Gwt code:

try
{
    RequestBuilder rb = new RequestBuilder(
    RequestBuilder.POST, "AuthenticationChecker.html");
            rb.sendRequest(null, new RequestCallback() 
            {
                public void onError(Request request, Throwable exception)
                {
                    RootPanel.get().add(new HTML("[error]" + exception.getMessage()));
                }
                public void onResponseReceived(Request request, Response response)
                {
                    RootPanel.get()
                   .add(new HTML("[success (" + response.getStatusCode() + "," + response.getStatusText() + ")]"));
                }
            }
    );
}
catch (Exception e)
{
    RootPanel.get().add(new HTML("Error sending request " + e.getMessage()));
}

AuthenticationChecker.html is a simple blank html page, from what I understand, as AuthenticationChecker.html requires role as admin, I should have got a 401 Unauthorized if remember me cookie was not present and a 200 OK if the user was authenticated and his cookie was present.

However, the output always shows: [success (200,OK)]

To cross check, i simply typed authenticaionChecker.html (without logging in) and it returned back to Login.html indicating that spring is indeed authenticating the user.

Am I doing something wrong here ?

+2  A: 

If you look at the tutorial, you'll see that a 401 is only returned when you're using Basic Authentication. With form-based authentication, you have to check the response text for an error message. For example:

public void onResponseReceived(Request request, Response response) {
    if (response.getStatusCode() != Response.SC_OK) {
        onError(request, new RequestException(response.getStatusText() + ":\n" + response.getText()));
        return;
    }

    if (response.getText().contains("Access Denied")) {
        Window.alert("You have entered an incorrect username or password. Please try again.");
    } else {
        // authentication worked, show a fancy dashboard screen
    }
}
Matt Raible
Thanks Dude, you saved my life !!!honestly, i didn't quite understand the diff between basic and form based authenticationan educated guess would be that the form based would be of the form:<form action="something"> <!-- some fields --> </form>I really do appreciate the answer given, matt, thanks for the effort!!
Salvin Francis