SpringBoot添加自定义拦截器的实现代码
在Controller层时,往往会需要校验或验证某些操作,而在每个Controller写重复代码,工作量比较大,这里在Springboot项目中,通过继承WebMvcConfigurerAdapter,添加拦截器。
1、WebMvcConfigurerAdapter源码
/*
*Copyright2002-2016theoriginalauthororauthors.
*
*LicensedundertheApacheLicense,Version2.0(the"License");
*youmaynotusethisfileexceptincompliancewiththeLicense.
*YoumayobtainacopyoftheLicenseat
*
*http://www.apache.org/licenses/LICENSE-2.0
*
*Unlessrequiredbyapplicablelaworagreedtoinwriting,software
*distributedundertheLicenseisdistributedonan"ASIS"BASIS,
*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
*SeetheLicenseforthespecificlanguagegoverningpermissionsand
*limitationsundertheLicense.
*/
packageorg.springframework.web.servlet.config.annotation;
importjava.util.List;
importorg.springframework.format.FormatterRegistry;
importorg.springframework.http.converter.HttpMessageConverter;
importorg.springframework.validation.MessageCodesResolver;
importorg.springframework.validation.Validator;
importorg.springframework.web.method.support.HandlerMethodArgumentResolver;
importorg.springframework.web.method.support.HandlerMethodReturnValueHandler;
importorg.springframework.web.servlet.HandlerExceptionResolver;
/**
*Animplementationof{@linkWebMvcConfigurer}withemptymethodsallowing
*subclassestooverrideonlythemethodsthey'reinterestedin.
*
*@authorRossenStoyanchev
*@since3.1
*/
publicabstractclassWebMvcConfigurerAdapterimplementsWebMvcConfigurer{
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidconfigurePathMatch(PathMatchConfigurerconfigurer){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidconfigureContentNegotiation(ContentNegotiationConfigurerconfigurer){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidconfigureAsyncSupport(AsyncSupportConfigurerconfigurer){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidconfigureDefaultServletHandling(DefaultServletHandlerConfigurerconfigurer){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddFormatters(FormatterRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddCorsMappings(CorsRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddViewControllers(ViewControllerRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidconfigureViewResolvers(ViewResolverRegistryregistry){
}
/**
*{@inheritDoc}
*
Thisimplementationisempty.
*/
@Override
publicvoidaddArgumentResolvers(ListargumentResolvers){
}
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidaddReturnValueHandlers(ListreturnValueHandlers){
}
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidconfigureMessageConverters(List>converters){
}
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidextendMessageConverters(List>converters){
}
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidconfigureHandlerExceptionResolvers(ListexceptionResolvers){
}
/**
*{@inheritDoc}
*Thisimplementationisempty.
*/
@Override
publicvoidextendHandlerExceptionResolvers(ListexceptionResolvers){
}
/**
*{@inheritDoc}
*Thisimplementationreturns{@codenull}.
*/
@Override
publicValidatorgetValidator(){
returnnull;
}
/**
*{@inheritDoc}
*
Thisimplementationreturns{@codenull}.
*/
@Override
publicMessageCodesResolvergetMessageCodesResolver(){
returnnull;
}
}
可以看出,该类还能配置其他很多操作,例如异常处理,跨域请求等配置。
2、自动义Web配置类
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
publicclassWebMvcConfigextendsWebMvcConfigurerAdapter{
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
}
@Bean
publicMyInterceptorgetMyInterceptor(){
returnnewMyInterceptor();
}
}
如果需要添加多个拦截器,InterceptorRegistryregistry.addInterceptor方法
publicInterceptorRegistrationaddInterceptor(HandlerInterceptorinterceptor){
InterceptorRegistrationregistration=newInterceptorRegistration(interceptor);
this.registrations.add(registration);
returnregistration;
}
registrations是个数组结构,可以添加多个
3、自动义拦截器
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.web.method.HandlerMethod;
importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter;
publicclassMyInterceptorextendsHandlerInterceptorAdapter{
finalLoggerlogger=LoggerFactory.getLogger(getClass());
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{
//拦截操作
returntrue;
}
}
总结
以上所述是小编给大家介绍的SpringBoot添加自定义拦截器的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!