如何定义SpringMVC框架拦截器呢?

SpringMVC 中的 Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆等操作。

SpringMVC 拦截器的定义方式

  • 实现接口

  • 继承适配器

案例实操

实现接口

实现 HandlerInterceptor 接口方式定义我们的拦截器代码如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class MyInterceptor implements HandlerInterceptor{

/**

  • preHandle 在请求方法拦截前执行
  • 返回 true 代表对当前请求进行放行处理

    /

    @Override

    public boolean preHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler) throws Exception {

    System.out.println("action 之前执行!!!");

    return true; //继续执行 action

    }

    /
    *
  • 请求执行后,生成视图前执行

    /

    @Override

    public void postHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler,

    ModelAndView modelAndView) throws Exception {

    System.out.println("Action 执行之后,生成视图之前执行!!");

    }

    /
    *
  • 在请求方法执行后进行拦截

    */

    @Override

    public void afterCompletion(HttpServletRequest request,

    HttpServletResponse response, Object handler, Exception ex)

    throws Exception {

    System.out.println("方法执行后进行拦截。。释放资源。。。");

    }

    } </pre>

对应配置有两种方式:

配置方式一(拦截所有请求配置方式):

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><mvc:interceptors>


<bean class="com.xxx.interceptors.MyInterceptor" />

</mvc:interceptors> </pre>

配置方式二(拦截指定请求配置方式)

mvc:interceptors

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><mvc:interceptor>

<mvc:mapping path="/test/*.do" />

<bean class="com.xxx.interceptors.MyInterceptor" />

</mvc:interceptor>

</mvc:interceptors> </pre>

效果:

请求地址: http://localhost:8080/springmvc03/test/hello.do

控制台打印结果信息:

[图片上传失败...(image-4fb3e4-1607912467620)]

多个拦截器配置(多个拦截器组成一个拦截器链 ,栈式结构 123 321 退出):

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><mvc:interceptors>

<mvc:interceptor>



<mvc:mapping path="/" />

<bean class="com.xxx.interceptors.MyInterceptor" />

</mvc:interceptor>

<mvc:interceptor>



<mvc:mapping path="/
" />

<bean class="com.xxx.interceptors.MyInterceptor2" />

</mvc:interceptor>

</mvc:interceptors> </pre>

继承适配器

继承 HandlerInterceptorAdapter 方式定义拦截器(实际上最终还是 HandlerInterceptor 接口实现)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class MyInterceptor2 extends HandlerInterceptorAdapter{

/**

  • 重写 preHandle 请求执行前执行
    */

    @Override

    public boolean preHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler) throws Exception {

    System.out.println("请求前执行。。。");

    return true;

    }

    } </pre>

扩展

使用拦截器完成用户是否登录请求验证拦截器定义:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class LoginInterceptor implements HandlerInterceptor{

/**

  • 方法拦截前执行

    /

    @Override

    public boolean preHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler) throws Exception {

    User user= (User) request.getSession().getAttribute("user");

    /
    *
  • 判断 uri 是否包含路径
  • 包含就放行

    /

    if(request.getRequestURI().indexOf("userLogin.do")>-1){

    return true;

    }

    /
    *
  • 判断 session user 是否为空

    /

    if(null==user){

    response.sendRedirect(request.getContextPath()+"/login.jsp");

    return false;

    }

    return true; //继续执行 action

    }

    @Override

    public void postHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler,

    ModelAndView modelAndView) throws Exception {

    System.out.println("Action 执行之后,生成视图之前执行!!");

    }

    /
    *
  • 在方法执行后进行拦截

    */

    @Override

    public void afterCompletion(HttpServletRequest request,

    HttpServletResponse response, Object handler, Exception ex)

    throws Exception {

    System.out.println("方法执行后进行拦截。。释放资源。。。");

    }

    } </pre>

UserController 类

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**

*

* @author Administrator

* 模拟 用户操作

*/

@Controller

@RequestMapping("/user")

public class UserLoginController {

@RequestMapping("/userLogin")

public ModelAndView userLogin(HttpServletRequest request){

ModelAndView mv=new ModelAndView();

User user=new User();

user.setUserName("xxx");

user.setUserPwd("123456");

request.getSession().setAttribute("user", user);

mv.setViewName("success");

return mv;

}

@RequestMapping("/addUser")

public ModelAndView addUser(){

System.out.println("添加用户记录。。。");

ModelAndView mv=new ModelAndView();

mv.setViewName("success");

return mv;

}

@RequestMapping("/delUser")

public ModelAndView delUser(){

ModelAndView mv=new ModelAndView();

mv.setViewName("success");

return mv;

}

@RequestMapping("/updateUser")

public ModelAndView updateUser(){

ModelAndView mv=new ModelAndView();

mv.setViewName("success");

return mv;

}
} </pre>

拦截器 xml 配置

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<mvc:interceptors>

<mvc:interceptor>

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

<bean class="com.xxx.interceptors.LoginInterceptor" />

</mvc:interceptor>

</mvc:interceptors> </pre>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容