首页 > 编程知识 正文

springboot之HandlerInterceptor拦截器入门

时间:2023-05-04 15:17:16 阅读:199675 作者:1328

springboot之HandlerInterceptor拦截器入门 前言HandlerInterceptor简介WebMvcConfigurer简介使用步骤pom加入如下依赖编写一个拦截器,实现HandlerInterceptor接口编写一个类,实现WebMvcConfigurer 接口结果

前言

下面提供主要的代码,其中涉及控制器代码、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

使用步骤 编写一个拦截器,实现HandlerInterceptor接口编写一个类,在该类中激活拦截器,配置拦截规则。具体操作是在该类实现 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;@Componentpublic 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;@Configurationpublic 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五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。