项目结构:
基于SpringBoot2.3
在SpringBoot中,已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来,那么在SpringBoot中对于Web场景的位置当然在WebMvcAutoConfiguration中,类似于其它的场景都是 xxxAutoConfiguration,因此只需要观察xxxAutoConfiguration类中到底添加了什么或者配置了哪些组件即可。
在WebMvcAutoConfiguration中关于静态资源的是其中的静态内部类WebMvcAutoConfigurationAdapter中对静态资源的映射做了设置
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
//所有 /webjars /**
//都会去 classpath:/META-INF/resources/webjars/ 寻找对应的资源
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).
addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).
setCachePeriod(this.getSeconds(cachePeriod)).
setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//设置静态资源问价夹的映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern})
.addResourceLocations(WebMvcAutoConfiguration
.getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(this.getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
}
}
所有 /webjars /** , 都会去 classpath:/META-INF/resources/webjars/ 寻找对应的资源;
而在this.resourceProperties.getStaticLocations()中它会去类 ResourceProperties 中获取对应的文件夹
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
以上就是SpringBoot在静态资源上的默认配置,因此我们只需要在对应的文件夹下创建web资源即可被自动访问,并且SpringBoot的web项目会自动访问index.html页面。
编写拦截器Interceptor
下面代码是判断用户是否登录的拦截器;
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
if(attribute != null){
//登录状态
//保存登录的用户
loginUser.set(attribute);
return true;
}else{
//未登录
response.sendRedirect("http://xxxx:xxxx/login.html");
return false;
}
}
}
编写Web配置类并添加创建好的拦截器
注意:一定要实现 WebMvcConfigurer 接口,其中该接口中有默认的方法 addInterceptors ,看其方法名称就很容易理解,就是用来添加拦截器使用的;
@Configuration
public class OrderWebConfiguration implements WebMvcConfigurer {
@Autowired
//将自己配置的拦截器自动注入进来
private LoginUserInterceptor userInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加拦截的请求,这里是所有请求,但是排除所有的静态资源,防止静态资源调用拦截器
registry.addInterceptor(userInterceptor).addPathPatterns("/**")
.excludePathPatterns("/static/confirm/**","/static/detail/**","/static/list/**","/static/pay/**");
}
@Override
//配置该项目中的静态资源,这是能否被识别的关键
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
}
最后,静态资源其实可以在Nginx中做动静分离的处理,这样也可以减轻访问该项目的压力。