欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 17:57:03
在controller层时,往往会需要校验或验证某些操作,而在每个controller写重复代码,工作量比较大,这里在springboot项目中 ,通过继承webmvcco...

在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添加自定义拦截器的实现代码,希望对大家有所帮助