springboot之HandlerInterceptor拦截器入门

前言

下面提供主要的代码,其中涉及控制器代码、application配置文件文章中没有给出,适合对springboot有基础的同学

HandlerInterceptor简介

HandlerInterceptor是SpringMVC中为拦截器提供的接口,类似于Servlet开发中的过滤器Filter,用于处理器进行预处理和后处理,这个接口中需要有三个方法重写。

preHandle:controller执行之前
postHandle:controller执行之后,且页面渲染之前调用
afterCompletion:最终执行方法,一般用于清理资源

WebMvcConfigurer简介

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口;

在Spring Boot 1.5版本靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport

使用步骤

  1. 编写一个拦截器,实现HandlerInterceptor接口
  2. 编写一个类,在该类中激活拦截器,配置拦截规则。具体操作是在该类实现 WebMvcConfigurer 接口,然后重写addInterceptors()方法,在方法中调用InterceptorRegistry.addInterceptor()方法,把刚才编写的那个拦截器对象作为参数加进去

下面进行代码实战

pom加入如下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

编写一个拦截器,实现HandlerInterceptor接口

/* 
 * 这里只是在拦截器中写了输出代码
 * 如果真实项目中会在拦截器中实现验证客户是否登陆等操作
 * 例如如果验证客户访问该页面需要登陆,但是未登陆
 * 就可以在 preHandle 方法中跳到登陆页面
 */
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyinInterceptor implements HandlerInterceptor{

//在Controller执行之前调用,如果返回false,controller不执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        System.out.println("***preHandle***");
        return true;
    }

//controller执行之后,且页面渲染之前调用
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("***postHandle***");
    }

//页面渲染之后调用,一般用于资源清理操作
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("***afterCompletion***");
    }
}

编写一个类,实现WebMvcConfigurer 接口

重写addInterceptors()方法,在方法中调用InterceptorRegistry.addInterceptor()方法,把刚才编写的那个拦截器作为参数加进去,同时配置拦截规则

import com.example.demo.interceptor.MyinInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyBlogWebMvcConfigurer implements WebMvcConfigurer{
    @Autowired
    MyinInterceptor myinInterceptor;

    public void addInterceptors(InterceptorRegistry registry){
    	//这时如果访问“/”路径就会触发拦截器
        registry.addInterceptor(myinInterceptor).addPathPatterns("/");
//      如果需要配置多个路径
//      registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/").excludePathPatterns("/admin/login").excludePathPatterns("/admin/dist/**").excludePathPatterns("/admin/plugins/**");
    }
}

结果

访问:localhost:8080(不同配置不同访问路径,我这里端口8080),控制台会输出

***preHandle***
***postHandle***
***afterCompletion***
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值