SpringBoot添加自定义拦截器的实现代码

 更新时间:2018年09月19日 15:29:34   作者:紫薇帝星的故事  
这篇文章主要介绍了SpringBoot添加自定义拦截器的实现代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

在Controller层时,往往会需要校验或验证某些操作,而在每个Controller写重复代码,工作量比较大,这里在Springboot项目中 ,通过继承WebMvcConfigurerAdapter,添加拦截器。

1、WebMvcConfigurerAdapter源码

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.web.servlet.config.annotation;
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
/**
 * An implementation of {@link WebMvcConfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 *
 * @author Rossen Stoyanchev
 * @since 3.1
 */
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addFormatters(FormatterRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation returns {@code null}.
   */
  @Override
  public Validator getValidator() {
    return null;
  }
  /**
   * {@inheritDoc}
   * <p>This implementation returns {@code null}.
   */
  @Override
  public MessageCodesResolver getMessageCodesResolver() {
    return null;
  }
}

可以看出,该类 还能配置其他很多操作,例如异常处理,跨域请求等配置。

2、自动义Web配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
  }
  @Bean
  public MyInterceptor getMyInterceptor(){
    return new MyInterceptor();
  }
}

  如果需要添加多个拦截器,InterceptorRegistry registry.addInterceptor方法

public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
    InterceptorRegistration registration = new InterceptorRegistration(interceptor);
    this.registrations.add(registration);
    return registration;
  }

registrations是个数组结构,可以添加多个

3、自动义拦截器

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class MyInterceptor extends HandlerInterceptorAdapter {
  final Logger logger = LoggerFactory.getLogger(getClass());
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //拦截操作
    return true;
  }
}

总结

以上所述是小编给大家介绍的SpringBoot添加自定义拦截器的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • java调用Hbase报错解决方法

    java调用Hbase报错解决方法

    这篇文章主要为大家介绍了java调用Hbase报错解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Java中的自旋锁spinlock详解

    Java中的自旋锁spinlock详解

    这篇文章主要介绍了Java中的自旋锁spinlock详解,自旋锁就是循环尝试获取锁,不会放弃CPU时间片,减伤cup上下文切换,缺点是循环会消耗cpu,需要的朋友可以参考下
    2024-01-01
  • Springboot整合PageOffice 实现word在线编辑保存功能

    Springboot整合PageOffice 实现word在线编辑保存功能

    这篇文章主要介绍了Springboot整合PageOffice 实现word在线编辑保存,本文以Samples5 为示例文件结合示例代码给大家详细介绍,需要的朋友可以参考下
    2021-08-08
  • java动态构建数据库复杂查询教程

    java动态构建数据库复杂查询教程

    这篇文章主要介绍了java动态构建数据库复杂查询的实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2021-11-11
  • Java实现重定向过程中添加请求头信息

    Java实现重定向过程中添加请求头信息

    在Java中,我们经常需要使用网络请求来与服务器进行通信,在进行网络请求时,有时我们需要在重定向过程中添加请求头信息,本文将介绍如何使用Java在重定向过程中添加请求头,并提供相应的代码示例,
    2023-10-10
  • idea 配置checkstyle详细步骤

    idea 配置checkstyle详细步骤

    checkstyle是提高代码质量,检查代码规范的很好用的一款工具,本文简单介绍一下集成的步骤,并提供一份完整的checkstyle的代码规范格式文件,以及常见的格式问题的解决方法,需要的朋友可以参考下
    2023-11-11
  • Java将String字符串带括号转成List的简单方法

    Java将String字符串带括号转成List的简单方法

    Java中我们有时需要对现有的字符串进行切割并转化成一个List集合,这篇文章主要给大家介绍了关于Java将String字符串带括号转成List的简单方法,需要的朋友可以参考下
    2023-03-03
  • maven 指定version不生效的问题

    maven 指定version不生效的问题

    这篇文章主要介绍了maven 指定version不生效的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java容器ArrayList知识点总结

    Java容器ArrayList知识点总结

    本篇文章给大家分享了Java容器ArrayList的相关知识点,对此有需要的朋友可以跟着学习参考下。
    2018-05-05
  • Java中break、continue、return在for循环中的使用

    Java中break、continue、return在for循环中的使用

    这篇文章主要介绍了break、continue、return在for循环中的使用,本文是小编收藏整理的,非常具有参考借鉴价值,需要的朋友可以参考下
    2017-11-11

最新评论