說(shuō)明:
在自己寫(xiě)一個(gè)spring boot demo的時(shí)候,想著根據(jù)session的過(guò)期時(shí)間來(lái)返回登錄頁(yè)面,在自定義完攔截器以后功能可以實(shí)現(xiàn),但是靜態(tài)資源卻無(wú)法訪問(wèn),通過(guò)查找文檔發(fā)現(xiàn)spring boot 1.x和2.x是不一樣,所以特別記錄一下,留給后面的同學(xué)避免踩坑.
1.x和2.x的靜態(tài)資源訪問(wèn)區(qū)別
- 1.x的resources/static目錄下的靜態(tài)資源可以直接訪問(wèn),并且訪問(wèn)路徑上不用帶static,當(dāng)有配置自定義HandlerInterceptor攔截器時(shí),請(qǐng)求靜態(tài)資源路徑不會(huì)被攔截止吐。
- 2.x的如果自定義HandlerInterceptor攔截器時(shí)訪問(wèn)靜態(tài)資源就會(huì)被同步攔截,這樣為了實(shí)現(xiàn)session過(guò)期而跳轉(zhuǎn)登錄頁(yè)面功能就會(huì)受影響.
項(xiàng)目的目錄
}O31{R6DQ}AQZLRQR0WVU`D.png
自定義攔截器
/**
* UserSecurityInterceptor
* Created with IntelliJ IDEA.
* Author: yangyongkang
* Date: 2018/8/22
* Time: 14:20
*/
@Component
public class UserSecurityInterceptor implements HandlerInterceptor {
@Autowired
private RedisTemplate<String, Serializable> redisCacheTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
UserModel info = (UserModel) redisCacheTemplate.opsForValue().get(request.getSession().getId());
if (info == null || StringUtils.isEmpty(info)) {
response.sendRedirect(request.getContextPath() + "/view/login");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
}
}
- 因?yàn)槲业膕ession是放在redis里面的,所以只需要從redis里面取即可.
配置訪問(wèn)路徑及靜態(tài)資源
/**
* 登陸攔截控制類
* Created with IntelliJ IDEA.
* Author: yangyongkang
* Date: 2018/8/22
* Time: 14:17
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private UserSecurityInterceptor securityInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(securityInterceptor);
// 排除配置
addInterceptor.excludePathPatterns("/error");
addInterceptor.excludePathPatterns("/static/**");//排除靜態(tài)資源
addInterceptor.excludePathPatterns("/view/login");
addInterceptor.excludePathPatterns("/login/check");
// 攔截配置
addInterceptor.addPathPatterns("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//
}
}
- 這樣有一個(gè)問(wèn)題,不明白為啥要這樣設(shè)計(jì),因?yàn)槿绻坏┛刂破鲀?nèi)有static開(kāi)頭的訪問(wèn)路徑的話,攔截器就不會(huì)攔截了.
頁(yè)面加載方式
//第一種在thymeleaf模版內(nèi)部
<link type="text/css" th:href="@{/static/css/page/bootstrap.min.css}" rel="stylesheet">
<link type="text/css" th:href="@{/static/css/page/font-awesome.css}" rel="stylesheet">
<link type="text/css" th:href="@{/static/css/page/animate.css}" rel="stylesheet">
<link type="text/css" th:href="@{/static/css/page/style.css}" rel="stylesheet">
<link type="text/css" th:href="@{/static/css/page/login.css}" rel="stylesheet">
//第二種
<link href="/static/css/page/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/page/font-awesome.css" rel="stylesheet">
<link href="/static/css/page/animate.css" rel="stylesheet">
<link href="/static/css/page/style.css" rel="stylesheet">
最后特別注意一下
- 在spring boot2.x中已經(jīng)無(wú)法再使用WebMvcConfigurationAdapter,官方聲明已過(guò)時(shí).
- 可以繼承WebMvcConfigurationSupport或者實(shí)現(xiàn)WebMvcConfigurer
implements WebMvcConfigurer :
//不會(huì)覆蓋@EnableAutoConfiguration關(guān)于WebMvcAutoConfiguration的配置
@EnableWebMvc + implements WebMvcConfigurer :
// 會(huì)覆蓋@EnableAutoConfiguration關(guān)于WebMvcAutoConfiguration的配置
extends WebMvcConfigurationSupport :
//會(huì)覆蓋@EnableAutoConfiguration關(guān)于WebMvcAutoConfiguration的配置
extends DelegatingWebMvcConfiguration :
//會(huì)覆蓋@EnableAutoConfiguration關(guān)于WebMvcAutoConfiguration的配置