0

0

SpringBoot添加自定义拦截器的方法实现(代码)

不言

不言

发布时间:2018-09-20 15:32:51

|

2760人浏览过

|

来源于php中文网

原创

本篇文章给大家带来的内容是关于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}
     * 

This implementation is empty. */ @Override public void configurePathMatch(PathMatchConfigurer configurer) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addFormatters(FormatterRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addInterceptors(InterceptorRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addCorsMappings(CorsRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addViewControllers(ViewControllerRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addArgumentResolvers(List argumentResolvers) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void addReturnValueHandlers(List returnValueHandlers) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureMessageConverters(List> converters) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void extendMessageConverters(List> converters) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void configureHandlerExceptionResolvers(List exceptionResolvers) { } /** * {@inheritDoc} *

This implementation is empty. */ @Override public void extendHandlerExceptionResolvers(List exceptionResolvers) { } /** * {@inheritDoc} *

This implementation returns {@code null}. */ @Override public Validator getValidator() { return null; } /** * {@inheritDoc} *

This implementation returns {@code null}. */ @Override public MessageCodesResolver getMessageCodesResolver() { return null; } }

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

2、自动义Web配置类

企业黄页模块 for PHPCMS9 GBK 正式版
企业黄页模块 for PHPCMS9 GBK 正式版

PHPCMS V9采用OOP(面向对象)方式进行基础运行框架搭建。模块化开发方式做为功能开发形式。框架易于功能扩展,代码维护,优秀的二次开发能力,可满足所有网站的应用需求。 PHPCMS V9企业黄页主要特色1、模型自定义,支持模型添加、修改、删除、导出、导入功能;2、模型字段自定义,支持模型字段添加、修改、删除、禁用操作;3、分类无限添加,支持批量多级添加;4、新增附件字段功能,实现相同模型,不

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

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

1

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

6

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

28

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

3

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

1

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Redis6入门到精通超详细教程
Redis6入门到精通超详细教程

共47课时 | 5.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号