自定义过滤器:
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);
}
});