36 lines
1.3 KiB
Java
36 lines
1.3 KiB
Java
package com.example.vuedemov20.Token;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
//入口拦截,设置哪些接口需要拦截或不拦截(保护后端接口 防止未经授权的访问)
|
|
@Configuration
|
|
public class IntercepterConfig implements WebMvcConfigurer {
|
|
private final TokenInterceptor tokenInterceptor;
|
|
|
|
// 构造方法
|
|
public IntercepterConfig(TokenInterceptor tokenInterceptor) {
|
|
this.tokenInterceptor = tokenInterceptor;
|
|
}
|
|
|
|
@Override
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
//excludePathPatterns用来配置不需要拦截的路径
|
|
List<String> excludePath = new ArrayList<>();//List用来保存所有不需要拦截的路径
|
|
excludePath.add("/register"); //注册
|
|
excludePath.add("/login"); //登录
|
|
|
|
|
|
|
|
registry.addInterceptor(tokenInterceptor)//添加名为tokenInterceptor的拦截器
|
|
.addPathPatterns("/**") //指定拦截所有路径
|
|
.excludePathPatterns(excludePath);//排除不需要拦截的路径
|
|
WebMvcConfigurer.super.addInterceptors(registry);
|
|
|
|
}
|
|
}
|