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);
}
});
shiro 自定义过滤器,拦截过期session的请求,并且以ajax形式返回的更多相关文章
- Shiro自定义过滤器
项目中需要所有首次登录的用户必须修改密码才可使用系统,项目采用的是Shiro框架. 突然想到了配置文件org.apache.shiro.spring.web.ShiroFilterFactoryBea ...
- Spring Cloud Gateway自定义过滤器实战(观测断路器状态变化)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- shiro自定义拦截url
在实际项目上,我们针对不同的用户(guste,user,admin,mobile user)等等,需要进入不同的页面,比如,手机端用户需要进入Mobile/这个路径下的,这个时候,我们需要自定义拦截u ...
- Shiro 自定义登陆、授权、拦截器
Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...
- shiro使用框架,自定义过滤器
1.shiro配置文件配置 <!-- Shiro Filter --> <bean id="shiroFilter" class="org.apache ...
- .net core MVC 通过 Filters 过滤器拦截请求及响应内容
前提: 需要nuget Microsoft.Extensions.Logging.Log4Net.AspNetCore 2.2.6: Swashbuckle.AspNetCore 我暂时用的是 ...
- Filter 快速开始 异步Servlet 异步请求 AsyncContext 异步线程 异步派发 过滤器拦截
[web.xml] <filter> <filter-name>normalFilter</filter-name> <filter-class>net ...
- shiro的过滤器
shiro的过滤器也是不多的我们可以自定义的方法,它的继承体系如下: 另外UserFilter是继承于AccessControlFilter 1.NameableFilter NameableFilt ...
- shiro 权限过滤器 -------(1)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBEAAAJRCAIAAACcEbhqAAAgAElEQVR4nO3dv67sVtkHYEefhIKUIC ...
随机推荐
- Oracle官方文档
Oracle DBA 10g 两日速成课程 http://www.oracle.com/webfolder/technetwork/cn/tutorials/obe/db/10g/r2/2day_db ...
- 安装lr时无法将值Disable Script Debugger 写入注册表
1. 运行“regedit”打开注册表编辑器. 2. 右键点击权限.HKEY_CURRENT_USER-Softwart-Microsoft-Internet Explorer-Main 3. 勾选e ...
- Citrix Port(常用端口)
组件 类型 端口 描述 CitrixLicenseServer 许可管理器守护程序 TCP 27000 处理初始接触点的许可证要求(Lmadmin.exe) 思杰供应商守护程序 TCP 7 ...
- iPhone开发之在UINavigationBar上使用UISegmentedControl制作
UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 7.0 ...
- js中的getBoundingClientRect()函数
在Chrome上,此函数返回结果包含x,y,它们的取值对应于left,top.但是x,y这种表示方式非常不直观(需要先理解x轴和y轴的方向),而left,top则清晰无歧义地表达了元素的位置.正是因此 ...
- 【LeetCode】207. Course Schedule (2 solutions)
Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some cours ...
- 极客先锋 如何生成git的公钥和私钥
一. Git windows 客服端(MsysGit)下载 下载地址:http://code.google.com/p/msysgit/ 二.从开始菜单中找到Git 点击Git Bash 弹出命令行窗 ...
- 【Algorithm】自顶向下的归并排序
一. 算法描述 自顶向下的归并排序:采用分治法进行自顶向下的程序设计方式,分治法的核心思想就是分解.求解.合并. 先将长度为N的无序序列分割平均分割为两段 然后分别对前半段进行归并排序.后半段进行归并 ...
- C# 连接 mySQL 出现 GUID 应包含带 4 个短划线的 32 位数 问题
C# 连接 mySQL 出现 GUID 应包含带 4 个短划线的 32 位数 问题 在连接字符串中加入 Old Guids=true; 如:server=localhost;userid=root;p ...
- Failed to resolve: 之一
摘要:编译不通过提示错误如下:gradle文件里边对应:解决方案:在gradle文件里边加上.+,解决后gradle文件如下图所示:然后编译就能通过. 解决方案: 在gradle文件里边加上.+,解决 ...