create a SuccessAuthenticationHandler and FailureAuthenticationHandler

public class AjaxSuccessAuthenticationHandler implements
AuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
response);

Writer out = responseWrapper.getWriter();
Map<String, Object> authResponse = new HashMap<String, Object>();
authResponse.put("success", true);
authResponse.put("user", SecurityContextHolder.getContext()
.getAuthentication().getName());
List<String> authorities = new ArrayList<String>();
for (GrantedAuthority auth : SecurityContextHolder.getContext()
.getAuthentication().getAuthorities()) {
authorities.add(auth.getAuthority());
}
authResponse.put("authorities", authorities);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, authResponse);
out.close();

}

}


public class AjaxAuthenticationFailureHandler implements
AuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException {
        HttpServletResponseWrapper responseWrapper = new
HttpServletResponseWrapper(response);
        Writer out = responseWrapper.getWriter();
        out.write("{ success: false, errors:{reason: 'Login failed. Try
again.'}} ");
        out.close();
}

}

And your spring Security Context File

<beans:bean
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
id="usernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="filterProcessesUrl" value="/j_spring_security_check"
/>
<beans:property name="authenticationSuccessHandler"
ref="successHandler" />
<beans:property name="authenticationFailureHandler"
ref="failureHandler" />
</beans:bean>

<beans:bean id="successHandler"
class="mil.jtcoic.tb.epik.bender.server.security.AjaxSuccessAuthenticationHandler"
/>

<beans:bean id="failureHandler"
class="mil.jtcoic.tb.epik.bender.server.security.AjaxAuthenticationFailureHandler"
/>
On Wed, Dec 22, 2010 at 12:20 PM, Alberto <[email protected]> wrote:

> Hi Travis,
>
>  Is there any way that you can provide examples of your solution?
>
> Thanks !
>
> On Dec 21, 8:19 am, Travis Camechis <[email protected]> wrote:
> > instead of using that I created my own custom Spring Security
> > SuccessHandlers and Failure Handlers that returns JSON back to the
> client.
> >  I then let the client handle the place management based on
> > success:true/false.  At this point I can also send credentials back in
> the
> > JSON as well.
> >
> >
> >
> >
> >
> >
> >
> > On Tue, Dec 21, 2010 at 9:15 AM, asianCoolz <[email protected]>
> wrote:
> > > I use gwt requestBuilder to query server result, if server-side spring
> > > checked user is not authenticated, it will forward to <form-login
> > > login-page="/gwtapplication.html#!login"       , but gwt is not
> > > forwarded to that page.  see below
> >
> > > requestBuilder.setCallback(new RequestCallback() {
> >
> > >            @Override
> > >            public void onError(final Request request, final Throwable
> > > exception) {
> > >                resultCallback.onFailure(exception);
> > >            }
> >
> > >            @Override
> > >            public void onResponseReceived(final Request request,
> > >                    final Response response) {
> >
> > >                if(response.getHeader("Content-
> > > Type").toLowerCase().equals("text/html".toLowerCase()))
> > >                {
> >
> > >                //response.getText() is
> > >                /**
> > >                  Expires Thu, 01 Jan 1970 00:00:00 GMT
> > >                  Set-Cookie JSESSIONID=1emk892yva1e9;Path=/
> > >                  Locationhttp://
> 127.0.0.1:8888/gwtapplication.html#!login
> > >                 Content-Length 0
> > >                 Server Jetty(6.1.x)
> >
> > >               **/
> >
> > >                }
> >
> > >            }
> > >        });
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Google Web Toolkit" group.
> > > To post to this group, send email to
> [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<google-web-toolkit%[email protected]><google-web-toolkit%2Bunsubs
> [email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/google-web-toolkit?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to