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

SpringMVC之拦截器的的配置和使用的更多相关文章

  1. springmvc中拦截器的定义和配置

    package com.hope.interceptor;import org.springframework.lang.Nullable;import org.springframework.web ...

  2. SpringMVC利用拦截器防止SQL注入

    引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...

  3. SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】

    注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处.假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

  4. 基于注解风格的Spring-MVC的拦截器

    基于注解风格的Spring-MVC的拦截器 Spring-MVC如何使用拦截器,官方文档只给出了非注解风格的例子.那么基于注解风格如何使用拦截器呢? 基于注解基本上有2个可使用的定义类,分别是Defa ...

  5. SpringMVC 学习-拦截器 HandlerInterceptor 类

    一.拦截器 HandlerInterceptor 类的作用 SpringMVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 二.怎么使用呢? 1. ...

  6. SpringMVC—Struts2拦截器学习网址整理

    引自:http://blog.csdn.net/wp1603710463/article/details/49982683 SpringMVC—Struts2拦截器学习网址整理 最近项目中遇到权限相关 ...

  7. JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用

    JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...

  8. 8.学习springmvc的拦截器

    一.springmvc拦截器介绍和环境搭建 1.介绍: 过滤器:servlet中的一部分,可以拦截所有想要访问的资源. 拦截器:SpringMVC框架中的,只能在SpringMVC中使用并且只能过滤控 ...

  9. 【SpringMVC】拦截器

    一.概述 1.1 拦截器的异常场合 1.2 拦截器中的方法 二.示例 2.1 定义两个拦截器 2.2 配置拦截器 2.3 执行顺序 三.拦截器应用 3.1 需求 3.2 用户登陆及退出功能开发 3.3 ...

随机推荐

  1. 论文翻译——Lattice indexing for spoken term detection

    第II节简要介绍与本文有关的先前工作第III节介绍文中使用的定义以及术语 第IV节介绍如何从原始ASR lattices中生成倒排索引结构 第V节详细介绍了ASR结构以及实验使用的数据 第VI节提供了 ...

  2. 找出链表中倒数第K个结点

    思路:两个指针,也是快指针和慢指针,先让快指针走k -1步,这时慢指针开始和快指针一起走到尾部.慢指针停止的点就是倒数第k个节点. public static ListNode findCountDo ...

  3. vue keep-alive内置缓存组件

    1.当组件在keep-alive被切换时将会执行activeted和deactiveted两个生命周期 2.inlude 正则表达式或字符串 ,只有符合条件的组件会被缓存 exclude正则表达式或字 ...

  4. windows上安装zip版mongodb

    版本3.4:现将mongodb解压,再选择一个位置创建data文件夹并在其下创建db文件夹和log文件夹 然后编写mongod.cfg文件,注意这里用的yaml格式,对空格很敏感,并且要注意mongo ...

  5. 【easy】108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  6. C# 常用类型校验Validate

    using System.Text; using System.Text.RegularExpressions; namespace 落地页测试代码 { public class Validate { ...

  7. 关于mvc中传递匿名对象,view中无法解析

    最近做项目用到MVC,发现用linq查询得到的数据是匿名类型对象,通过模型绑定.或者ViewBag.ViewData进行数据传递后,View解析报错:“object 未包含xx的定义”: 没找到好的解 ...

  8. 简单使用zabbix监控nginx是否存活

    1.在agent端修改主配置文件 vim /etc/zabbix/zabbix_agentd.conf ........ ........ UserParameter=nginx.status[*], ...

  9. Vuejs自定义select2指令

    在做select2插件的时候遇到一些坑,最终解决如下: Vue.directive('select2', { inserted: function (el, binding, vnode) { var ...

  10. pyqt win32发送QQ消息

    标题应该改为:python+win32发送QQ消息,全程使用python套个pyqt壳. 其实代码来自: http://blog.csdn.net/suzyu12345/article/details ...