spring拦截器覆盖_spring拦截器(interceptor)简介

1. 拦截器用途

(1)拦截未登录用户直接访问某些链接

(2)拦截日志信息

(3)拦截非法攻击,比如sql注入

2. 涉及jar、类

(1)spring-webmvc.jar

(2)HandlerInterceptor(org.springframework.web.servlet:接口)、

AsyncHandlerInterceptor(org.springframework.web.servlet:接口)、

HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter:抽象类)

3.业务类

(1)实现(implements) 实现HandlerInterceptor接口或者子接口

(2)继承(extends) 继承HandlerInterceptor接口子类(抽象类)

(3)涉及的方法

preHandle、postHandle、afterCompletion

4.测试代码

LoginInterceptor

1 public booleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handlerthrowsException {2   LOGGER.info("--------preHandle-------" +request.getRequestURI());3   HttpSession session =request.getSession();4   String login_account = String.valueOf(session.getAttribute(CommonConstants.SESSION_KEY+session.getId()));5   if(!request.getRequestURI().contains("/baselogin/")){6     if (StringUtils.isBlank(login_account) || "null".equalsIgnoreCase(login_account)) {7       response.sendRedirect("/baselogin/loginPage.htm");8       return false; 9     }10   }11   return true;12 }13

14 public voidpostHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throwsException {15   LOGGER.info("--------postHandle-------" +request.getRequestURI());

16 }17

18 public voidafterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throwsException {19   LOGGER.info("--------afterCompletion-------" +request.getRequestURI());20 }21

22 public voidafterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {23   LOGGER.info("--------afterConcurrentHandlingStarted-------" +request.getRequestURI());24 }

执行顺序:preHandle -> controller -> postHandle -> afterCompletion

配置:

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值