Shiro Ajax请求权限不满足,拦截后解决方案

这里有一个前提,我们知道Ajax不能做页面redirectforward跳转,所以Ajax请求假如没登录,那么这个请求给用户的感觉就是没有任何反应,而用户又不知道用户已经退出了.解决办法:

后台代码 

         String requestedWith = request.getHeader("x-requested-with");
		 if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
		     //ajax 请求后登录授权失效的情况
		     PrintWriter printWriter = response.getWriter();
	             response.setStatus(499);
	             printWriter.close();
	             return null;
		 }

前端ajax 方法 

/*ajax 异步请求  授权失效后跳到登录页面*/  
	$.ajaxSetup({
        statusCode: {
            499: function (data) {
			window.location="${ctx}/loginJsp";
        }
    }

原理: 通过重写ajax的状态码作为判断依据, 后台设置499,前端会ajax做相应的处理 

具体可以查询 ajax重写状态码相关文章 

猜你喜欢

转载自blog.csdn.net/JqueryTomcat/article/details/83030282