拦截器与过滤器的区别:拦截器只能拦截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>

最新文章

  1. [译]初识.NET Core &amp; ASP.NET Core
  2. [leetcode]Binary Tree Maximum Path Sum
  3. Oracle 与 entity framework 6 的配置,文档
  4. [0x00 用Python讲解数据结构与算法] 概览
  5. 如何在Oracle中导入dmp文件
  6. shell expr的用法
  7. Linux常用的一些命令
  8. 一个平时写程序通用的Makefile样例
  9. php pod
  10. 【和我一起学Python吧】Python3.0与2.X版本的区别
  11. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
  12. Objective-C 链式编程思想
  13. docker学习笔记3:镜像操作(查找和下载)
  14. Python3基础 list() 将一个字符串转换成列表
  15. wxpython分割窗研究(解决sashPosition=0无效的BUG)
  16. underscore.js 源码阅读 一 整体结构
  17. [FJOI2016]建筑师
  18. eolinker使用初体验(一)
  19. 使用easyui搭建网页架子
  20. git连接不上远程仓库---visualstudio提交代码报错:no upstream configured for branch &#39;master&#39;

热门文章

  1. 伯努利数学习笔记&amp;&amp;Luogu P3711 仓鼠的数学题
  2. 只有try和finally,没有catch
  3. bootstrap时间格式化
  4. python numpy 间的的数据变算公式
  5. 服务发现 consul cluster 的搭建【转】
  6. 什么时候Python的List,Tuple最后一个Item后面要加上一个逗号
  7. oracle,mysql,sql server三大数据库的事务隔离级别查看方法
  8. TensorFlow object detection API
  9. HBuilderx中编译sass文件
  10. scikit-learn中机器学习模型比较(逻辑回归与KNN)