国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

SpringBoot添加自定義攔截器的實(shí)現(xiàn)代碼

2024-07-14 08:42:22
字體:
供稿:網(wǎng)友

在Controller層時(shí),往往會(huì)需要校驗(yàn)或驗(yàn)證某些操作,而在每個(gè)Controller寫重復(fù)代碼,工作量比較大,這里在Springboot項(xiàng)目中 ,通過繼承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;  }}

可以看出,該類 還能配置其他很多操作,例如異常處理,跨域請(qǐng)求等配置。

2、自動(dòng)義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;@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {  @Override  public void addInterceptors(InterceptorRegistry registry) {    registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");  }  @Bean  public MyInterceptor getMyInterceptor(){    return new MyInterceptor();  }}

  如果需要添加多個(gè)攔截器,InterceptorRegistry registry.addInterceptor方法

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

registrations是個(gè)數(shù)組結(jié)構(gòu),可以添加多個(gè)

3、自動(dòng)義攔截器

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;  }}

總結(jié)

以上所述是小編給大家介紹的SpringBoot添加自定義攔截器的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芦溪县| 墨脱县| 吕梁市| 孝昌县| 桂东县| 萝北县| 拜城县| 黄山市| 大同市| 平和县| 开平市| 舞阳县| 丽水市| 册亨县| 土默特右旗| 凤凰县| 永安市| 连城县| 陆良县| 广水市| 工布江达县| 曲松县| 嘉善县| 仁怀市| 苏尼特左旗| 秭归县| 西林县| 寿阳县| 休宁县| 木兰县| 六安市| 青浦区| 巴林右旗| 通化市| 玛沁县| 曲松县| 彩票| 澄城县| 铜鼓县| 衡山县| 铜鼓县|