|
|
package com.config;
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
|
|
|
|
|
import com.interceptor.AuthorizationInterceptor;
|
|
|
|
|
|
/**
|
|
|
* Spring MVC 全局拦截器与静态资源配置类
|
|
|
*
|
|
|
* <p>核心职责:
|
|
|
* <ul>
|
|
|
* <li>注册并配置自定义拦截器(如认证拦截器)</li>
|
|
|
* <li>解决静态资源访问问题(覆盖默认配置时需手动指定)</li>
|
|
|
* <li>定制化Web MVC行为</li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* <p><b>注意事项:</b>
|
|
|
* 继承 {@link WebMvcConfigurationSupport} 会覆盖Spring Boot的自动配置,
|
|
|
* 需要手动处理静态资源映射,否则会导致静态资源无法访问。
|
|
|
*
|
|
|
* @author YourName
|
|
|
* @version 1.0
|
|
|
* @since 2023-10-01
|
|
|
*/
|
|
|
@Configuration // 声明此类为Spring配置类
|
|
|
public class InterceptorConfig extends WebMvcConfigurationSupport {
|
|
|
|
|
|
/**
|
|
|
* 创建授权拦截器Bean
|
|
|
*
|
|
|
* <p>使用 {@code @Bean} 注解将拦截器实例纳入Spring容器管理,
|
|
|
* 确保拦截器中的依赖注入能正常工作。
|
|
|
*
|
|
|
* @return 初始化完成的授权拦截器实例
|
|
|
*/
|
|
|
@Bean
|
|
|
public AuthorizationInterceptor getAuthorizationInterceptor() {
|
|
|
return new AuthorizationInterceptor();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 注册拦截器并配置拦截规则
|
|
|
*
|
|
|
* <p><b>拦截配置说明:</b>
|
|
|
* <ul>
|
|
|
* <li>{@code .addPathPatterns("/**")} - 拦截所有请求路径</li>
|
|
|
* <li>{@code .excludePathPatterns("/static/**")} - 排除静态资源路径</li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* <p>典型应用场景:
|
|
|
* <ul>
|
|
|
* <li>检查API请求的Token有效性</li>
|
|
|
* <li>记录请求日志</li>
|
|
|
* <li>权限验证</li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* @param registry 拦截器注册器
|
|
|
*/
|
|
|
@Override
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
// 核心拦截器配置
|
|
|
registry.addInterceptor(getAuthorizationInterceptor())
|
|
|
.addPathPatterns("/**") // 拦截所有路径
|
|
|
.excludePathPatterns("/static/**"); // 排除静态资源
|
|
|
|
|
|
// 保留父类注册逻辑(可选)
|
|
|
super.addInterceptors(registry);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置静态资源处理规则
|
|
|
*
|
|
|
* <p><b>必要性:</b>当覆盖 {@code WebMvcConfigurationSupport} 时,
|
|
|
* Spring Boot的默认静态资源配置会失效,必须手动指定资源映射。
|
|
|
*
|
|
|
* <p><b>资源位置说明:</b>
|
|
|
* <table border="1">
|
|
|
* <tr><th>路径模式</th><th>物理位置</th><th>典型内容</th></tr>
|
|
|
* <tr><td>/**</td><td>classpath:/resources/</td><td>XML配置文件</td></tr>
|
|
|
* <tr><td>/**</td><td>classpath:/static/</td><td>CSS/JS/图片</td></tr>
|
|
|
* <tr><td>/**</td><td>classpath:/admin/</td><td>后台管理界面资源</td></tr>
|
|
|
* <tr><td>/**</td><td>classpath:/front/</td><td>前端界面资源</td></tr>
|
|
|
* <tr><td>/**</td><td>classpath:/public/</td><td>公共资源</td></tr>
|
|
|
* </table>
|
|
|
*
|
|
|
* <p><b>特殊说明:</b>
|
|
|
* 在Spring Boot中,通常不需要此配置,除非需要完全自定义MVC行为。
|
|
|
*
|
|
|
* @param registry 资源处理器注册器
|
|
|
*/
|
|
|
@Override
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
// 配置静态资源映射
|
|
|
registry.addResourceHandler("/**") // 匹配所有URL路径
|
|
|
.addResourceLocations("classpath:/resources/") // 资源位置1
|
|
|
.addResourceLocations("classpath:/static/") // 资源位置2
|
|
|
.addResourceLocations("classpath:/admin/") // 资源位置3
|
|
|
.addResourceLocations("classpath:/front/") // 资源位置4
|
|
|
.addResourceLocations("classpath:/public/"); // 资源位置5
|
|
|
|
|
|
// 保留父类配置(可选)
|
|
|
super.addResourceHandlers(registry);
|
|
|
}
|
|
|
}
|