How to get login page in the main window in Spring after a jQuery Ajax call -
i have ajax based web application written in spring , jquery. authentication use spring security.i start application individual login page (not integrated applcation window) , want end after session time out. however, when session times out during ajax call login page (served spring security) inserted in "div" result of ajax call supposed come. therefore, end situation when have login page in sub-part of application window,instead of 1 singe login page. searching solution on server side,not in jquery, if possible. initial idea doing this:
public class ajaxawareloginurlauthenticationentrypoint extends loginurlauthenticationentrypoint { public void commence(final httpservletrequest request, final httpservletresponse response, final authenticationexception authexception) throws ioexception, servletexception { if (request.getheader("x-requested-with").equals("xmlhttprequest")) { redirectstrategy redirectstrategy = new defaultredirectstrategy(); redirectstrategy.sendredirect(request, response, "/j_spring_security_logout"); } else { super.commence(request, response, authexception); } } the idea force loginurlauthenticationentrypoint make logout call, hoping whole page replaced login form beeing returned. not work. should correct way handle issue?thank you!
edited - 1 soluton: ok, haven't manage without client side code kryger recommended. worked me
1) @ server side implementing custom loginurlauthenticationentrypoint:
public class ajaxawareloginurlauthenticationentrypoint extends loginurlauthenticationentrypoint { public void commence(final httpservletrequest request, final httpservletresponse response, final authenticationexception authexception) throws ioexception, servletexception { if (request.getheader("x-requested-with").equals("xmlhttprequest")) { setuseforward(true); response.setheader("reload-to-loginpage", "yes"); super.commence(request, response, authexception); } else { super.commence(request, response, authexception); } } note using
setuseforward(true); to request forwarded header.
2) on client side simply
$(document).ajaxsuccess(function(event, request, settings) { if (request.getresponseheader('reload-to-loginpage') === 'yes') { window.location.replace('/yourwebsite'); } }); and possibly same .ajaxerror() unless handle errors in special way. please,let me know if can improve code!
Comments
Post a Comment