继承WebMvcConfigurationSupport
的配置类
下面例子是引入第三方包中实现的拦截器,所以注入为bean先。若是使用已经注入为bean的拦截器,直接@Autowired或@Resource引入即可。
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class InterceptorConf extends WebMvcConfigurationSupport {
@Value("login.interceptor.enviroment.id")
String enviromentId;
/*@Value("login.interceptor.none.annotation.permission.level")
String noneAnnotationPermissionLevel;*/
@Bean
com.xxxx.xdcs.spring.interceptor.XdcsSpringMvcInterceptor xdcsSpringMvcInterceptor(){
return new XdcsSpringMvcInterceptor();
}
@Bean
com.xxxx.passport.sso.interceptor.LoginInterceptor loginInterceptor(){
LoginInterceptor loginInterceptor = new LoginInterceptor();
loginInterceptor.setEnvironmentId(enviromentId);
loginInterceptor.setNoneAnnotationPermissionLevel(0);
return loginInterceptor;
}
/**
* 表示这些配置的是静态文件所处路径, 不用拦截
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**")
.addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 自定义拦截器,添加拦截路径和排除拦截路径
registry.addInterceptor(xdcsSpringMvcInterceptor()).addPathPatterns("/**");
registry.addInterceptor(loginInterceptor()).addPathPatterns("/**").excludePathPatterns("/");
super.addInterceptors(registry);
}
}