How to handle an expired session using spring-security and jQuery?

I am using spring-security and jQuery in my application. On the main page, content is dynamically loaded into tabs through Ajax. And everything is fine, but sometimes I have a login page in my tab, and if I type in the credentials, I will be redirected to the tabless content page.

Therefore, I would like to deal with this situation. I would just write a global handler for all ajax responses that window.location.reload () will do if we need to authenticate.

+1
source share
1 answer

I ran into the same problem. Please see the decision I made and see if it is useful to you.

My log page used the old model controller instead of spring 3.0 annotations.

I registered the global ajax error handler as follows

jQuery(document).ajaxError( function(event, request, ajaxOptions, thrownError) { try { var result = getJsonObject(request.responseText);//Convert the json reply to json object if (result.timeout) { window.location.reload(); } } catch (e) { // Ignore this error } }); 

Then in my login controller, I check if the original request was an ajax request or the x-requested-with header was not used. If it was an ajax request, I return a json response saying {"timeout" : true} .

 private boolean isAjaxRequest(HttpServletRequest request) { boolean isAjaxRequest = false; SavedRequest savedRequest = (SavedRequest) request.getSession() .getAttribute( DefaultSavedRequest.SPRING_SECURITY_SAVED_REQUEST_KEY); if (savedRequest != null) { List<String> ajaxHeaderValues = savedRequest .getHeaderValues("x-requested-with"); for (String value : ajaxHeaderValues) { if (StringUtils.equalsIgnoreCase("XMLHttpRequest", value)) { isAjaxRequest = true; } } } return isAjaxRequest; } ............. ............. ............. if (isAjaxRequest(request)) { Map<String, Object> model = new HashMap<String, Object>(); model.put("timeout", true); return new ModelAndView(new JSONView(model));//Returns a json object. } else { //return login page } 

JSONView implementation example

 import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import org.springframework.web.servlet.View; import com.greytip.common.json.CougarJsonConfig; public class JSONView implements View { private JSON jsonObject; private JsonConfig jsonConfig; private String value; public JSONView(Object value) { this(); jsonObject = JSONObject.fromObject(value, jsonConfig); } public JSONView(List<?> value) { this(); jsonObject = JSONArray.fromObject(value, jsonConfig); } public JSONView(String value) { this(); this.value = value; } public JSONView() { jsonConfig = new JsonConfig();//Your json config } @SuppressWarnings("unchecked") public void render(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception { if (jsonObject != null) { jsonObject.write(response.getWriter()); } if (value != null) { response.getWriter().write(value); } } public String getContentType() { return "text/json"; } } 

It uses the json-lib library to convert objects to json format.

Spring 3.0 has very good jackson library support , you can try using it.

+1
source

All Articles