2011-08-30 103 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