参考:https://www.cnblogs.com/SimonHu1993/p/8360701.html
一定要将 拦截器组件 交给spring容器进行管理,这样才能注入配置值,或者注入bean:
package com.thunisoft.maybeemanagementcenter.interceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebAppConfigure extends WebMvcConfigurerAdapter {
@Value("${thunisoft.microservice.mvc.interceptor.includePath:/**}")
private String includePath;
@Value("${thunisoft.microservice.mvc.interceptor.excludePath:/login/*}")
private String excludePath;
@Override
public void addInterceptors(InterceptorRegistry registry) {
String[] excludePaths = excludePath.split(",");
String[] includePaths = includePath.split(",");
InterceptorRegistration ir = registry.addInterceptor(loginInterceptor())
.addPathPatterns(includePaths)
.excludePathPatterns(excludePaths);
super.addInterceptors(registry);
}
@Bean
public LoginInterceptor loginInterceptor() {
return new LoginInterceptor();
}
}
重点代码:
@Bean public LoginInterceptor loginInterceptor() { return new LoginInterceptor(); }
registry.addInterceptor(loginInterceptor())