在进行springboot的web项目实战,在添加登陆拦截器之前,前台页面样式引用是好的,添加之后css样式加载不到了。
解决报错的方法:分别放开对报错静态资源的拦截
报错拦截器代码
@Bean
public WebMvcConfigurer webMvcConfigurer() {
WebMvcConfigurer wmc = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//静态资源*.css,*.js,因springboot已经做好资源映射,所以无需处理静态资源
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/user/login","/");
}
};
return wmc;
正确拦截器代码:
@Bean
public WebMvcConfigurer webMvcConfigurer() {
WebMvcConfigurer wmc = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//静态资源*.css,*.js,因springboot已经做好资源映射,所以无需处理静态资源,注意放开报错的静态资源
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/user/login","/","/webjars/**","/img/**","/**/*.css","/**/*.svg");
}
};
return wmc;
from lj 2020.05.25