自定義攔截器類
public class SessionInterceptor extends HandlerInterceptorAdapter {
創(chuàng)新互聯(lián)是一家專業(yè)提供三門峽企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5建站、小程序制作等業(yè)務(wù)。10年已為三門峽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
public SessionInterceptor() {
// TODO Auto-generated constructor stub
}
private List excludedUrls;
//通過屬性注冊不需要過濾的url list
public void setExcludedUrls(List excludedUrls) {
this.excludedUrls = excludedUrls;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String requestUrl = request.getRequestURI();
//排除不需要過濾的URL
for(String url:excludedUrls) {
if(requestUrl.endsWith(url)) {
return true;
}
}
//獲取當(dāng)前的會(huì)話session
HttpSession session = request.getSession();
if(session.getAttribute("userid") == null) {
//若登錄session過期或不存在就跳轉(zhuǎn)到login頁面
request.getRequestDispatcher("/login.jsp").forward(request, response);
return false;
}
return true;
}
}
springmvc-servlet.xml 攔截器注冊
HandlerInterceptorAdapter不能攔截WEB-INF目錄以外的jsp文件;若需攔截默認(rèn)index.jsp;可以將index.jsp移動(dòng)目錄到WEB-INF下即可;