• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-05) 20次浏览 已收录 0个评论

这篇文章主要介绍了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><p>This implementation is empty. */ @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addFormatters(FormatterRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addInterceptors(InterceptorRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addCorsMappings(CorsRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addViewControllers(ViewControllerRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { } /** * {@inheritDoc} * </p><p>This implementation is empty. */ @Override public void addArgumentResolvers(List argumentResolvers) { } /** * {@inheritDoc} * <p>This implementation is empty. */ @Override public void addReturnValueHandlers(List returnValueHandlers) { } /** * {@inheritDoc} * <p>This implementation is empty. */ @Override public void configureMessageConverters(List converters) { } /** * {@inheritDoc} * <p>This implementation is empty. */ @Override public void configureHandlerExceptionResolvers(List exceptionResolvers) { } /** * {@inheritDoc} * <p>This implementation is empty. */ @Override public void extendHandlerExceptionResolvers(List exceptionResolvers) { } /** * {@inheritDoc} * <p>This implementation returns {@code null}. */ @Override public Validator getValidator() { return null; } /** * {@inheritDoc} * </p><p>This implementation returns {@code null}. */ @Override public MessageCodesResolver getMessageCodesResolver() { return null; } }

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

2、自动义Web配置类

 import org.s<em style="color:transparent">来源[email protected]搞@^&代*@码网</em>pringframework.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添加自定义拦截器的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!

以上就是SpringBoot添加自定义拦截器的实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot添加自定义拦截器的实现代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址