zoukankan      html  css  js  c++  java
  • shiro 自定义过滤器,拦截过期session的请求,并且以ajax形式返回

    自定义过滤器:

    public class CustomFormAuthenticationFilter extends FormAuthenticationFilter {
    
        @Override
        protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
            if (isLoginRequest(request, response)) {
                if (isLoginSubmission(request, response)) {
                    return executeLogin(request, response);
                } else {
                    // 放行 allow them to see the login page ;)
                    return true;
                }
            } else {
                HttpServletRequest httpRequest = WebUtils.toHttp(request);
                
                if (ShiroFilterUtils.isAjax(httpRequest)) {
                    
                    HttpServletResponse httpServletResponse = WebUtils.toHttp(response);  
                    httpServletResponse.sendError(ShiroFilterUtils.HTTP_STATUS_SESSION_EXPIRE);
                     
                    return false;
        
                } else {  
                    saveRequestAndRedirectToLogin(request, response); 
                }  
    
                return false;
            }
        }
        
        /**
         * 判断ajax请求
         * @param request
         * @return
         */
        boolean isAjax(HttpServletRequest request){
            return  (request.getHeader("X-Requested-With") != null  && "XMLHttpRequest".equals( request.getHeader("X-Requested-With").toString())   ) ;
        }
        
    }

    封装ajax

    var Error = function () {
        
        return {
            // 初始化各个函数及对象
            init: function () {
    
            },
            
            // 显示或者记录错误
            displayError: function(response, ajaxOptions, thrownError) {
                if (response.status == 404) {// 页面没有找到
                    pageContent.load($("#hdnContextPath").val() + "/page/404.action");
                } else if (response.status == 401) {// session过期
                    SweetAlert.errorSessionExpire();
                } else if (response.status == 507) {// 用户访问次数太频繁 
                    SweetAlert.error("您的访问次数太频繁, 请过一会再试...");
                } else {//其他错误
                    window.location = $("#hdnContextPath").val() + "/page/500.action";  
                }
                console.log(thrownError);
            }
    
        };
    
    }();
    
    jQuery(document).ready(function() {
        Error.init();
    });

    JS的引用处如下:

    App.blockUI();
        
        $.ajax({
            url: $("#hdnContextPath").val() + "/feedback/queryFeedBackDetail.action",
            type: "POST",
            async: false,
            data: {"feedbackId": feedbackId, "userId": userId, "status": status},
            success: function(data) {
                // 忽略
            },
            error: function (response, ajaxOptions, thrownError) {
                App.unblockUI();
                Error.displayError(response, ajaxOptions, thrownError);                
            }
        });
  • 相关阅读:
    对称的二元变量和不对称的二元变量之间的区别是什么?
    数据挖掘中ID3算法实现zz
    深入浅出谈数据挖掘zz
    JS动态生成表格后 合并单元格
    JS 点击元素发ajax请求 打开一个新窗口
    JS实现拖动div层移动
    简单的表格json控件
    JS添加标签效果
    JS模板引擎
    如何使用seajs+jQuery构建中型项目
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/7070229.html
Copyright © 2011-2022 走看看