首页 > Java > java教程 > Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

PHP中文网
发布: 2017-07-03 17:30:26
原创
1460 人浏览过

转载请注明出处: 

前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

1.拦截器定义

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

定义拦截器,实现HandlerInterceptor接口。接口中提供三个方法。

<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.interceptor;

</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletRequest;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletResponse;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.HandlerInterceptor;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.ModelAndView;

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> HandlerInterceptor1 <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{

    </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权
    </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">);
        </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行
        </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span>
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
    }

}</span>
登录后复制

2.拦截器配置

struts中是有一个大的拦截器链,它是一个共用的东西,可以把它添加到任何的action链接,都让它拦截。但是spring的拦截器不是全局的。

2.1针对某种mapping配置拦截器

springmvc拦截器针对HandlerMapping进行拦截设置,如果在某个HandlerMapping中设置拦截,经过该HandlerMapping映射成功的handler最终使用该拦截器。

<span style="color: #0000ff"><</span><span style="color: #800000">bean
    </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="interceptors"</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">list</span><span style="color: #0000ff">></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #0000ff">/></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">list</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">/></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">/></span>
登录后复制

一般不推荐使用。

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

springmvc可以配置类似全局的拦截器,springmvc框架将配置的类似全局的拦截器注入到每个HandlerMapping中。

    <span style="color: #008000"><!--</span><span style="color: #008000">拦截器 </span><span style="color: #008000">--></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span>
        <span style="color: #008000"><!--</span><span style="color: #008000">多个拦截器,顺序执行 </span><span style="color: #008000">--></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
            <span style="color: #008000"><!--</span><span style="color: #008000"> /**表示所有url包括子url路径 </span><span style="color: #008000">--></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span>
登录后复制

3.拦截测试

3.1测试需求

测试多个拦截器各个方法的执行时机。

3.2编写两个拦截器

 

3.3两个拦截器都放行

运行日志信息:

<span style="color: #000000">HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle

HandlerInterceptor2...postHandle
HandlerInterceptor1...postHandle

HandlerInterceptor2...afterCompletion
HandlerInterceptor1...afterCompletion</span>
登录后复制

总结:

preHandle方法按顺序执行,postHandle和afterCompletion按拦截器配置的逆向顺序执行。

3.4拦截器1放行,拦截器2不放行

运行日志信息:

<span style="color: #000000">HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle
HandlerInterceptor1...afterCompletion</span>
登录后复制

总结:

拦截器1放行,拦截器2的preHandle才会执行。

拦截器2的preHandle不放行,拦截器2的postHandle和afterCompletion不会执行。

只要有一个拦截器不放行,postHandle就不会执行。

3.5拦截器1不放行,拦截器2不放行

运行日志信息:

HandlerInterceptor1...preHandle
登录后复制

拦截器1的preHandle不放行,postHandle和afterCompletion不会执行。

拦截器1的preHandle不放行,拦截器2不执行。

4.小结

根据测试结果,对拦截器应用。

比如:统一日志处理拦截器,需要改拦截器preHandle一定要放行,且将它放在拦截器链中的第一位置。

比如:登录认证拦截器,放在拦截器链中第一个位置。权限校验拦截器,放在登录拦截器之后。(因为登录通过后才校验权限)

5.拦截器应用(实现登录认证)

5.1需求

(1)用户请求url

(2)拦截器进行拦截校验

  如果请求的url是公开地址(无需登录即可访问的url),让放行

  如果用户session不存在,跳转到登录页面。

  如果用户session存在,放行,继续操作。

5.2登录、退出controller方法

<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.controller;

</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSession;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Controller;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.bind.annotation.RequestMapping;

@Controller
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> LoginController {
    
    </span><span style="color: #008000">//</span><span style="color: #008000">登录</span>
    @RequestMapping("/login"<span style="color: #000000">)
    </span><span style="color: #0000ff">public</span> String login(HttpSession session, String username, String password) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{
        </span><span style="color: #008000">//</span><span style="color: #008000">调用service进行用户身份认证
        </span><span style="color: #008000">//</span><span style="color: #008000">...
        
        </span><span style="color: #008000">//</span><span style="color: #008000">在session中保存用户身份信息</span>
        session.setAttribute("username"<span style="color: #000000">, username);
        </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">;
    }
    
    </span><span style="color: #008000">//</span><span style="color: #008000">退出</span>
    @RequestMapping("/logout"<span style="color: #000000">)
    </span><span style="color: #0000ff">public</span> String logout(HttpSession session) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{
        </span><span style="color: #008000">//</span><span style="color: #008000">清除session</span>
<span style="color: #000000">        session.invalidate();
        </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">;
    }
    
}</span>
登录后复制

5.3登录认证拦截实现

5.3.1LoginInterceptor

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LoginInterceptor <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{

    </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权
    </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">);
        </span><span style="color: #008000">//</span><span style="color: #008000">获取请求的url</span>
        String url=<span style="color: #000000">request.getRequestURI();
        </span><span style="color: #008000">//</span><span style="color: #008000">判断url是否是公开地址(实际使用时要将公开地址配置到文件中)
        </span><span style="color: #008000">//</span><span style="color: #008000">这里公开地址是登录提交的地址</span>
        <span style="color: #0000ff">if</span>(url.indexOf("login.action")>=0<span style="color: #000000">){
            </span><span style="color: #008000">//</span><span style="color: #008000">如果进行登录提交,放行</span>
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
        }
        </span><span style="color: #008000">//</span><span style="color: #008000">判断session</span>
        HttpSession session=<span style="color: #000000">request.getSession();
        String username</span>=(String) session.getAttribute("username"<span style="color: #000000">);
        </span><span style="color: #0000ff">if</span>(username!=<span style="color: #0000ff">null</span><span style="color: #000000">){
            </span><span style="color: #008000">//</span><span style="color: #008000">身份存在,放行</span>
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
        }
        
        </span><span style="color: #008000">//</span><span style="color: #008000">执行到这里,表示用户身份需要认证,跳转登录页面</span>
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"<span style="color: #000000">).forward(request, response);
        
        </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行
        </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span>
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
    }

}</span>
登录后复制

5.3.2拦截器配置

classpath下springmvc.xml中配置:

如果此文对您有帮助,微信打赏我一下吧~ 

 

以上是Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板