使用springmvc的拦截器应用

版权声明:我的个人博客:http://www.zjhuiwan.cn。 https://blog.csdn.net/sunon_/article/details/84380302

Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理。

拦截器定义

实现HandlerInterceptor接口,如下:

拦截器配置

针对某种mapping配置拦截器

1

2

3

4

5

6

7

8

9

10

11

<bean

    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

    <property name="interceptors">

       <list>

           <ref bean="handlerInterceptor1"/>

           <ref bean="handlerInterceptor2"/>

       </list>

    </property>

</bean>

    <bean id="handlerInterceptor1" class="springmvc.intercapter.HandlerInterceptor1"/>

    <bean id="handlerInterceptor2" class="springmvc.intercapter.HandlerInterceptor2"/>

针对所有mapping配置全局拦截器

1

2

3

4

5

6

7

8

9

10

11

12

<!--拦截器 -->

<mvc:interceptors>

    <!--多个拦截器,顺序执行 -->

    <mvc:interceptor>

       <mvc:mapping path="/**"/>

       <bean class="cn.itcast.springmvc.filter.HandlerInterceptor1"></bean>

    </mvc:interceptor>

    <mvc:interceptor>

       <mvc:mapping path="/**"/>

       <bean class="cn.itcast.springmvc.filter.HandlerInterceptor2"></bean>

    </mvc:interceptor>

</mvc:interceptors>

正常流程测试

代码:

定义两个拦截器分别为:HandlerInterceptor1和HandlerInteptor2,每个拦截器的preHandler方法都返回true。

                                             

image.png

运行流程

HandlerInterceptor1..preHandle..

HandlerInterceptor2..preHandle..

HandlerInterceptor2..postHandle..

HandlerInterceptor1..postHandle..

HandlerInterceptor2..afterCompletion..

HandlerInterceptor1..afterCompletion..

中断流程测试

代码:

定义两个拦截器分别为:HandlerInterceptor1和HandlerInteptor2。

运行流程

HandlerInterceptor1的preHandler方法返回false,HandlerInterceptor2返回true,运行流程如下:

HandlerInterceptor1..preHandle..

从日志看出第一个拦截器的preHandler方法返回false后第一个拦截器只执行了preHandler方法,其它两个方法没有执行,第二个拦截器的所有方法不执行,且controller也不执行了。

HandlerInterceptor1的preHandler方法返回true,HandlerInterceptor2返回false,运行流程如下:

HandlerInterceptor1..preHandle..

HandlerInterceptor2..preHandle..

HandlerInterceptor1..afterCompletion..

从日志看出第二个拦截器的preHandler方法返回false后第一个拦截器的postHandler没有执行,第二个拦截器的postHandler和afterCompletion没有执行,且controller也不执行了。

总结:

preHandle按拦截器定义顺序调用

postHandler按拦截器定义逆序调用

afterCompletion按拦截器定义逆序调用

postHandler在拦截器链内所有拦截器返成功调用

afterCompletion只有preHandle返回true才调用

拦截器应用

用户身份认证

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Public class LoginInterceptor implements HandlerInterceptor{

    @Override

    Public boolean preHandle(HttpServletRequest request,

            HttpServletResponse response, Object handler) throws Exception {

        //如果是登录页面则放行

        if(request.getRequestURI().indexOf("login.action")>=0){

            return true;

        }

        HttpSession session = request.getSession();

        //如果用户已登录也放行

        if(session.getAttribute("user")!=null){

            return true;

        }

        //用户没有登录挑战到登录页面

        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);

        return false;

    }

}

用户登陆controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//登陆提交

    //userid:用户账号,pwd:密码

    @RequestMapping("/login")

    public String loginsubmit(HttpSession session,String userid,String pwd)throws Exception{

        

       //向session记录用户身份信息

       session.setAttribute("activeUser", userid);

        

       return "redirect:item/queryItem.action";

    }

     

    //退出

    @RequestMapping("/logout")

    public String logout(HttpSession session)throws Exception{

        

       //session过期

       session.invalidate();

        

       return "redirect:item/queryItem.action";

    }

猜你喜欢

转载自blog.csdn.net/sunon_/article/details/84380302