目的:使拦截器类UserInterceptor生效。
方法:
1.创建一个类 MvcConfig实现接口WebMvcConfigurer并加注解@Configuration。
2.实现addInterceptors()方法
@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class MvcConfig implements WebMvcConfigurer {
@Autowired
private JwtProperties jwtProperties;
@Bean
public UserInterceptor userInterceptor() {
return new UserInterceptor(jwtProperties);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//通过userInterceptor方法生成一个拦截器UserInterceptor在容器里,并设置成拦截一切路径(/**)
registry.addInterceptor(userInterceptor()).addPathPatterns("/**");
}
}