|
|
|
@ -0,0 +1,61 @@
|
|
|
|
|
// 声明当前文件所在的包路径,在 Java 项目中,包用于组织代码结构,这里表示该文件属于 com.config 包,有助于模块化管理和避免命名冲突
|
|
|
|
|
package com.config;
|
|
|
|
|
|
|
|
|
|
// 导入 Spring 框架中用于定义 Bean 的注解类。使用 @Bean 注解可以将一个方法返回的对象注册为 Spring 容器中的 Bean,供其他组件使用
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
// 导入 Spring 框架中用于标识配置类的注解类。被 @Configuration 注解的类相当于传统的 XML 配置文件,可在类中定义 Bean 和配置应用程序的各种组件
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
// 导入 Spring 框架中用于配置拦截器注册的接口类。通过实现该接口或继承相关支持类,可以向 Spring MVC 注册拦截器,控制请求的处理流程
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
|
|
// 导入 Spring 框架中用于配置静态资源处理器的接口类。可以使用该类来配置如何映射和处理静态资源,如 CSS、JavaScript、图片等
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
|
|
// 导入 Spring 框架中用于支持 Web MVC 配置的基类。继承该类可以对 Spring MVC 进行全面的配置,包括拦截器、静态资源、视图解析器等
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
|
|
|
|
|
|
|
|
|
// 导入自定义的拦截器类,该拦截器类用于处理请求的授权逻辑,在请求到达控制器之前进行权限检查等操作
|
|
|
|
|
import com.interceptor.AuthorizationInterceptor;
|
|
|
|
|
|
|
|
|
|
//拦截器配置类
|
|
|
|
|
//用于配置Spring MVC拦截器和静态资源处理
|
|
|
|
|
@Configuration // 标识这是一个Spring配置类
|
|
|
|
|
public class InterceptorConfig extends WebMvcConfigurationSupport {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建授权拦截器Bean
|
|
|
|
|
//@return AuthorizationInterceptor实例
|
|
|
|
|
@Bean
|
|
|
|
|
public AuthorizationInterceptor getAuthorizationInterceptor() {
|
|
|
|
|
return new AuthorizationInterceptor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置拦截器规则
|
|
|
|
|
//@param registry 拦截器注册器
|
|
|
|
|
@Override
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
// 注册授权拦截器并配置拦截规则
|
|
|
|
|
registry.addInterceptor(getAuthorizationInterceptor())
|
|
|
|
|
.addPathPatterns("/**") // 拦截所有请求路径
|
|
|
|
|
.excludePathPatterns("/static/**"); // 排除静态资源路径
|
|
|
|
|
super.addInterceptors(registry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置静态资源处理
|
|
|
|
|
//注意:在SpringBoot 2.0中自定义WebMvc配置会覆盖默认配置,
|
|
|
|
|
//需要手动添加静态资源路径配置
|
|
|
|
|
//@param registry 资源处理器注册器
|
|
|
|
|
@Override
|
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
// 配置静态资源访问路径
|
|
|
|
|
registry.addResourceHandler("/**") // 匹配所有URL路径
|
|
|
|
|
// 添加各类静态资源目录位置
|
|
|
|
|
.addResourceLocations("classpath:/resources/") // resources目录
|
|
|
|
|
.addResourceLocations("classpath:/static/") // static目录
|
|
|
|
|
.addResourceLocations("classpath:/admin/") // admin目录
|
|
|
|
|
.addResourceLocations("classpath:/img/") // img目录
|
|
|
|
|
.addResourceLocations("classpath:/front/") // front目录
|
|
|
|
|
.addResourceLocations("classpath:/public/"); // public目录
|
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
|
}
|
|
|
|
|
}
|