SpringMVC之拦截器的的配置和使用
2024-08-28 17:23:52
拦截器与过滤器的区别:拦截器只能拦截controller的请求,过滤器可以过滤所有请求
(1)实现HandlerInterceptor接口
在执行控制器中的方法之前执行preHandle()中的方法
package com.eu.weh.web; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.eu.weh.bean.User; /**
*
* @ClassName: LoginInterceptor
* @Description: 登录拦截器
* @author Administrator
* @date 2019年4月29日 上午10:31:25
*
*/
public class LoginInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception { //判断当前用户是否已经登录
HttpSession session = request.getSession();
User loginUser = (User) session.getAttribute("dbUser");
if (loginUser == null) {
String path = session.getServletContext().getContextPath();
response.sendRedirect(path+"/login");
return false;
}else {
return true;
}
} public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception { } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception { } }
(2)SpringMVC配置文件
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login" />
<mvc:exclude-mapping path="/doAJAXlogin" />
<mvc:exclude-mapping path="/bootstrap/**" />
<mvc:exclude-mapping path="/css/**" />
<mvc:exclude-mapping path="/fonts/**" />
<mvc:exclude-mapping path="/img/**" />
<mvc:exclude-mapping path="/jquery/**" />
<mvc:exclude-mapping path="/layer/**" />
<mvc:exclude-mapping path="/script/**" />
<mvc:exclude-mapping path="/ztree/**" />
<bean class="com.eu.weh.web.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
最新文章
- [译]初识.NET Core &; ASP.NET Core
- [leetcode]Binary Tree Maximum Path Sum
- Oracle 与 entity framework 6 的配置,文档
- [0x00 用Python讲解数据结构与算法] 概览
- 如何在Oracle中导入dmp文件
- shell expr的用法
- Linux常用的一些命令
- 一个平时写程序通用的Makefile样例
- php pod
- 【和我一起学Python吧】Python3.0与2.X版本的区别
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
- Objective-C 链式编程思想
- docker学习笔记3:镜像操作(查找和下载)
- Python3基础 list() 将一个字符串转换成列表
- wxpython分割窗研究(解决sashPosition=0无效的BUG)
- underscore.js 源码阅读 一 整体结构
- [FJOI2016]建筑师
- eolinker使用初体验(一)
- 使用easyui搭建网页架子
- git连接不上远程仓库---visualstudio提交代码报错:no upstream configured for branch &#39;master&#39;
热门文章
- 伯努利数学习笔记&;&;Luogu P3711 仓鼠的数学题
- 只有try和finally,没有catch
- bootstrap时间格式化
- python numpy 间的的数据变算公式
- 服务发现 consul cluster 的搭建【转】
- 什么时候Python的List,Tuple最后一个Item后面要加上一个逗号
- oracle,mysql,sql server三大数据库的事务隔离级别查看方法
- TensorFlow object detection API
- HBuilderx中编译sass文件
- scikit-learn中机器学习模型比较(逻辑回归与KNN)