SpringBoot添加自定义拦截器的方法实现(代码)
程序员文章站
2022-03-10 20:11:14
...
本篇文章给大家带来的内容是关于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添加自定义拦截器的方法实现(代码)的详细内容,更多请关注其它相关文章!
推荐阅读
-
Android ImageButton自定义按钮的按下效果的代码实现方法分享
-
iOS Xcode自定义代码块及迁移的实现方法
-
利用spring的拦截器自定义缓存的实现实例代码
-
android自定义RadioGroup可以添加多种布局的实现方法
-
element-ui 的el-button组件中添加自定义颜色和图标的实现方法
-
springboot 自定义异常并捕获异常返给前端的实现代码
-
使用Dubbo的SPI扩展机制实现自定义LoadBalance——方法一 修改Dubbo源代码
-
Android ImageButton自定义按钮的按下效果的代码实现方法分享
-
SpringMVC自定义拦截器登录检测功能的实现代码
-
springboot自定义拦截器的方法