|
|
@ -8,32 +8,70 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp
|
|
|
|
|
|
|
|
|
|
|
|
import com.interceptor.AuthorizationInterceptor;
|
|
|
|
import com.interceptor.AuthorizationInterceptor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 该类使用了Spring的配置注解 @Configuration,表示这是一个配置类
|
|
|
|
|
|
|
|
// 并且继承自WebMvcConfigurationSupport,用于对Spring Web MVC进行相关配置扩展
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
public class InterceptorConfig extends WebMvcConfigurationSupport{
|
|
|
|
public class InterceptorConfig extends WebMvcConfigurationSupport{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 @Bean 注解将方法返回的对象注册为Spring容器中的一个Bean
|
|
|
|
|
|
|
|
// 这里创建并返回一个AuthorizationInterceptor实例,用于后续的拦截器相关操作
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public AuthorizationInterceptor getAuthorizationInterceptor() {
|
|
|
|
public AuthorizationInterceptor getAuthorizationInterceptor() {
|
|
|
|
return new AuthorizationInterceptor();
|
|
|
|
return new AuthorizationInterceptor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类WebMvcConfigurationSupport的addInterceptors方法
|
|
|
|
|
|
|
|
// 目的是添加自定义的拦截器到Spring MVC的拦截器链中
|
|
|
|
|
|
|
|
// 此处添加了AuthorizationInterceptor拦截器,并设置其拦截的路径模式为"/**"(即所有路径)
|
|
|
|
|
|
|
|
// 同时排除了"/static/**"路径模式,也就是对静态资源相关的路径不进行拦截
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
registry.addInterceptor(getAuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
|
|
|
|
registry.addInterceptor(getAuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
|
|
|
|
super.addInterceptors(registry);
|
|
|
|
super.addInterceptors(registry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,要访问静态资源需要重写addResourceHandlers方法
|
|
|
|
* springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,要访问静态资源需要重写addResourceHandlers方法
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,
|
|
|
|
|
|
|
|
* 要访问静态资源需要重写addResourceHandlers方法。
|
|
|
|
|
|
|
|
* 此方法就是重写后的用于配置静态资源访问的方法,通过ResourceHandlerRegistry来注册静态资源的访问路径和对应位置。
|
|
|
|
|
|
|
|
* 这里将多个不同的类路径下的资源位置添加进来,使得这些位置下的静态资源可以通过对应的路径被访问到。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
|
|
|
// 为资源处理器添加路径匹配模式为"/**",表示匹配所有请求路径
|
|
|
|
registry.addResourceHandler("/**")
|
|
|
|
registry.addResourceHandler("/**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/resources/目录作为静态资源位置,意味着可以访问该目录下的静态资源
|
|
|
|
.addResourceLocations("classpath:/resources/")
|
|
|
|
.addResourceLocations("classpath:/resources/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/static/目录作为静态资源位置
|
|
|
|
.addResourceLocations("classpath:/static/")
|
|
|
|
.addResourceLocations("classpath:/static/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/admin/目录作为静态资源位置
|
|
|
|
.addResourceLocations("classpath:/admin/")
|
|
|
|
.addResourceLocations("classpath:/admin/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/img/目录作为静态资源位置
|
|
|
|
.addResourceLocations("classpath:/img/")
|
|
|
|
.addResourceLocations("classpath:/img/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/front/目录作为静态资源位置
|
|
|
|
.addResourceLocations("classpath:/front/")
|
|
|
|
.addResourceLocations("classpath:/front/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加类路径下的/public/目录作为静态资源位置
|
|
|
|
.addResourceLocations("classpath:/public/");
|
|
|
|
.addResourceLocations("classpath:/public/");
|
|
|
|
|
|
|
|
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|