【问题标题】:How to handle expired session using spring-security and jQuery?如何使用 spring-security 和 jQuery 处理过期会话?
【发布时间】:2011-11-06 18:13:23
【问题描述】:

我在我的应用程序中使用 spring-security 和 jQuery。主页使用通过 Ajax 将内容动态加载到选项卡中。一切正常,但有时我的标签页中有登录页面,如果我输入凭据,我将被重定向到没有标签页的内容页面。

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

【问题讨论】:

    标签: jquery ajax security spring session


    【解决方案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
            }
        });
    

    然后在我的登录控制器中,我使用 x-requested-with 标头检查原始请求是否是 ajax 请求。如果是 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 格式。

    Spring 3.0对jackson库的支持非常好,大家可以试试。

    【讨论】:

    • 您是否忘记为新的 JSONView(model) 添加 JSONView 类的实现
    猜你喜欢
    • 2011-03-21
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多