现有web系统替换成Spring Boot2框架 之7 拦截器

阅读更多

1. 自定义拦截器java类,继承HandlerInterceptorAdapter。例如LoginAnnotationInterceptor

 

2. 编写配置类,实现WebMvcConfigurer,重写addInterceptors方法,添加自定义的拦截器

例如:拦截URL 中的*.do *.action

 

@Configuration

public class SystemWebMvcConfig implements WebMvcConfigurer{

 

@Autowired

private LoginAnnotationInterceptor loginAnnotationInterceptor;


@Override

    public void configurePathMatch(PathMatchConfigurer configurer) {

        configurer.setUseRegisteredSuffixPatternMatch(true);

    }


@Override

public void addInterceptors(InterceptorRegistry registry) {

List excludeList = new ArrayList(); registry.addInterceptor(loginAnnotationInterceptor).addPathPatterns("/**/**/**.do").excludePathPatterns(excludeList);

}

}

 

注:

registry.addInterceptor(loginAnnotationInterceptor).addPathPatterns("/**/**/**.do").excludePathPatterns(excludeList);

“/**/**/**.do”,每一个/**代表一层目录,只用一个/**.do只能拦截“loginAct.do”无法拦截“bss/user/user!goList.do”。所以要拦截所有的.do请求需要按照最深的目录配置

你可能感兴趣的:(spring,boot,maven,拦截器,addInterceptors)