---
title: "现有web系统替换成Spring Boot2框架 之7 拦截器"
author: "sanry"
date: "2019-02-22T11:22:17Z"
tags: []
categories: ["编程语言"]
slug: "web-spring-boot2-7"
draft: false
lastmod: "2019-02-22T11:22:16Z"
id: 2437833
url: "https://sanry.iteye.com/blog/2437833"
---

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<String> excludeList = new ArrayList<String>(); registry.addInterceptor(loginAnnotationInterceptor).addPathPatterns("/**/**/**.do").excludePathPatterns(excludeList);

}

}
```

 

**注：**

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

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