guyue
2025-07-21 ad835011afaf88624e5b5f27b248c6b1089b7d8a
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
package com.linghu.config;
 
import com.linghu.utils.OpenCryptUtil;
import com.linghu.utils.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import javax.annotation.Resource;
 
@Configuration
public class WebConfig implements WebMvcConfigurer { // 实现WebMvcConfigurer
    @Resource
    private final OpenCryptUtil openCryptUtil;
    private final TokenInterceptor tokenInterceptor;
 
    // 构造器注入依赖
    public WebConfig(OpenCryptUtil openCryptUtil) {
        this.openCryptUtil = openCryptUtil;
        this.tokenInterceptor = new TokenInterceptor(openCryptUtil);
    }
 
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")// 拦截/api/开头的请求(按需调整)
                .excludePathPatterns( // 排除不需要拦截的路径(如登录、注册接口)
                        "/index/**",
                        "/auth/**",
                        "/swagger-ui.html",
                        "/swagger-resources/**",
                        "/v2/api-docs",
                        "/webjars/**"
 
                );
    }
}