自定义过滤器:

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);
}
});

shiro 自定义过滤器,拦截过期session的请求,并且以ajax形式返回的更多相关文章

  1. Shiro自定义过滤器

    项目中需要所有首次登录的用户必须修改密码才可使用系统,项目采用的是Shiro框架. 突然想到了配置文件org.apache.shiro.spring.web.ShiroFilterFactoryBea ...

  2. Spring Cloud Gateway自定义过滤器实战(观测断路器状态变化)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. shiro自定义拦截url

    在实际项目上,我们针对不同的用户(guste,user,admin,mobile user)等等,需要进入不同的页面,比如,手机端用户需要进入Mobile/这个路径下的,这个时候,我们需要自定义拦截u ...

  4. Shiro 自定义登陆、授权、拦截器

    Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...

  5. shiro使用框架,自定义过滤器

    1.shiro配置文件配置 <!-- Shiro Filter --> <bean id="shiroFilter" class="org.apache ...

  6. .net core MVC 通过 Filters 过滤器拦截请求及响应内容

    前提: 需要nuget   Microsoft.Extensions.Logging.Log4Net.AspNetCore   2.2.6: Swashbuckle.AspNetCore 我暂时用的是 ...

  7. Filter 快速开始 异步Servlet 异步请求 AsyncContext 异步线程 异步派发 过滤器拦截

    [web.xml] <filter> <filter-name>normalFilter</filter-name> <filter-class>net ...

  8. shiro的过滤器

    shiro的过滤器也是不多的我们可以自定义的方法,它的继承体系如下: 另外UserFilter是继承于AccessControlFilter 1.NameableFilter NameableFilt ...

  9. shiro 权限过滤器 -------(1)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBEAAAJRCAIAAACcEbhqAAAgAElEQVR4nO3dv67sVtkHYEefhIKUIC ...

随机推荐

  1. Nginx server之Nginx添加ssl支持

    //环境介绍 1.nginx服务器:10.10.54.157 2.配置nginx服务器,当监听到来自客户端www.zijian.com:80请求时,转到10.10.54.150:1500这个web服务 ...

  2. Definitaion of 'utsname' must be imported from module 'Darwin.POSIX.sys.utsname' before it is required

    https://stackoverflow.com/questions/34430354/objective-c-gettimeofday-must-be-imported

  3. 〖Linux〗使用sed命令修改小端(little endian)存储的数据

    #!/bin/bash - #=============================================================================== # # F ...

  4. mybatis generator(MyBatis的逆向工程)

    1创建数据表 如图所示:我的是在text数据库中创建了一个Student表,字段有id(int),   name(varchar),     age(int),    score(int) 2创建项目 ...

  5. Weblogic部署gbk编码的项目乱码

    问题描述: weblogic默认部署的项目编码utf-8,由于项目特殊编码gbk导致打开项目中文全是乱码! 心哇凉哇凉.... 问题解决: 01.调休jvm参数 xxx_domains/bin/set ...

  6. Newifi2(D1) 刷入pb-boot和breed的记录

    今天要给一个newifi d1刷系统时发现居然还是原厂的uboot, 使用uboot刷入rom时会进行校验拦截第三方的rom. 之前有刷过这个设备的, 但是已经完全记不清怎么处理的了. 查了一下, 这 ...

  7. Golang Json文件解析为结构体工具-json2go

    代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...

  8. ios中两个view动画切换

    @interface ViewController () @property(nonatomic,retain)UIView *redview; @property(nonatomic,retain) ...

  9. hduoj1073--Online Judge

    做道题,并没有太多的技巧,关键在与对Accepted,presented error 和wa的判断,第一步如果两者完全一样,那么很定是AC了 ,否则如果去掉多余换行,空格,制表后还有不同说明是数据 不 ...

  10. MATLAB 的向量,矩阵和阵列命令

    MATLAB 的向量,矩阵和阵列命令: