新建一個攔截器類,實現 org.springframework.web.servlet.HandlerInterceptor 接口,重寫preHandle、postHandle、afterCompletion方法分別是處理前、處理中、處理后。
public class RequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("目標方法執行前!"); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("目標方法執行時!"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("目標方法執行后!"); }
}
在配置類中添加該攔截器,如下:
方式一:根據后綴放行,這樣要是文件類型很多的話是不是就很麻煩呢
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //注冊TestInterceptor攔截器 InterceptorRegistration registration = registry.addInterceptor(new RequestInterceptor()); registration.addPathPatterns("/**"); //所有路徑都被攔截 registration.excludePathPatterns( //添加不攔截路徑 "/**/*.html", "/**/*.js", //js靜態資源 "/**/*.css", //css靜態資源 "/**/*.woff", "/**/*.ttf" ); } }
方式二:有個問題就是:靜態資源是默認static下的,請求路徑中沒有static,所以放行不成功
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login","/logout","/static/**"); }
方式三:就是直接配置攔截所有,然后在攔截器中對url篩選,只要是請求的靜態資源就直接返回true,但是感覺做了一些無用功
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registration.addInterceptor(new RequestInterceptor()).addPathPatterns("/**"); }
}
//攔截器中
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { boolean check = false; StringBuffer url = request.getRequestURL(); if(isStatic(url)){ check = true; }else{ Object auth = request.getSession().getAttribute("auth"); if(auth == null){ String scheme = request.getScheme(); String serverName = request.getServerName(); int port = request.getServerPort(); String context = request.getContextPath(); String path = scheme+"://"+serverName+":"+port+context+"/"; String str = "<script language='javascript'>alert('登錄狀態過期,請重新登陸!');" +"if (window != top){top.location.href = '"+ path +"login';}location.href='"+path+"login'" +"</script>"; response.setContentType("text/html;charset=UTF-8");// 解決中文亂碼 try { PrintWriter writer = response.getWriter(); writer.write(str); writer.flush(); writer.close(); } catch (Exception e) {e.printStackTrace();} }else{ check = true; } } return check; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } public boolean isStatic(StringBuffer url) { boolean result = false; String[] arr = { //定義一個需要放行的數組 "/login", "/css", "/images",//圖片 "/js" //js腳本 }; for (String a : arr) { if (url.indexOf(a) != -1) { result = true; } } return result; }
//excludePathPatterns放行是? 請求路徑中包含你所配置的就ok,還是你請求的是這個目錄下的資源就可以呢?