2011-08-30 142 views
1

我在我的应用程序中使用spring-security和jQuery。主页面通过Ajax动态地将加载内容动态添加到标签中。一切都可以,但是有时我的标签页里面有登录页面,如果输入凭据,我将被重定向到没有标签页的内容页面。如何使用spring-security和jQuery处理过期的会话?

所以我想处理这种情况。我想为所有的ajax响应编写一个全局处理程序,如果需要进行身份验证,它将执行window.location.reload()。

回答

1

我遇到了同样的问题。请参阅我采用的解决方案并检查它是否对您有用。

我的日志页面使用旧的模型控制器而不是Spring 3.0注释模型。

我注册了一个全球性的Ajax错误处理程序如下

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 
     } 
    }); 

然后在我的登录控制,我检查原始请求是否是一个Ajax请求或不使用x-requested-with头。如果这是一个Ajax请求,那么我返回一个json响应,说{"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

的样品实施
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"; 
    } 

} 

它使用json-lib库将对象转换成JSON格式。

春3.0有jackson库很好的支持,你可以尝试使用它。

+0

你忘了为新的JSONView(模型)添加类JSONView的实现吗? – Sankalp