kaiyu
2021-03-11 d21e551b42746e5c689c96e584042e418083ff9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.moral.api.config.mvc;
 
import com.moral.api.interceptor.ManageInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import java.util.ArrayList;
 
@Configuration
public class SpringMVCconfig implements WebMvcConfigurer{
 
    /**
    * @Description: 获取yaml中配置的不拦截路径
            * @Param: []
            * @return: java.util.ArrayList<java.lang.String>
            * @Author: 陈凯裕
            * @Date: 2021/3/9
            */
    @Bean
    @ConfigurationProperties("mvc.interceptor.exclude")
    public ArrayList<String> getExcludePath(){
        return new ArrayList<>();
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration regisration = registry.addInterceptor(new ManageInterceptor());
        ArrayList<String> excludePath = getExcludePath();
        regisration.addPathPatterns("/**/**");//设置拦截路径
        regisration.excludePathPatterns(excludePath);//设置不拦截路径
    }
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}