監聽器:listener是servlet規範中定義的一種特殊類。用於監聽servletContext、HttpSession和servletRequest等域對象的建立和銷燬事件。監聽域對象的屬性發生修改的事件。用於在事件發生前、發生後作一些必要的處理。其主要可用於如下方面:一、統計在線人數和在線用戶二、系統啓動時加載初始化信息三、統計網站訪問量四、記錄用戶訪問路徑。html
過濾器:Filter是Servlet技術中最實用的技術,Web開發人員經過Filter技術,對web服務器管理的全部web資源:例如Jsp, Servlet, 靜態圖片文件或靜態 html 文件等進行攔截,從而實現一些特殊的功能。例如實現URL級別的權限訪問控制、過濾敏感詞彙、壓縮響應信息等一些高級功能。它主要用於對用戶請求進行預處理,也能夠對HttpServletResponse進行後處理。使用Filter的完整流程:Filter對用戶請求進行預處理,接着將請求交給Servlet進行處理並生成響應,最後Filter再對服務器響應進行後處理。java
攔截器:Interceptor 在AOP(Aspect-Oriented Programming)中用於在某個方法或字段被訪問以前,進行攔截而後在以前或以後加入某些操做。好比日誌,安全等。通常攔截器方法都是經過動態代理的方式實現。能夠經過它來進行權限驗證,或者判斷用戶是否登錄,或者是像12306 判斷當前時間是不是購票時間。web
三大器在springboot中使用時,首先實現相應的接口定義類,而後經過配置類將其加入到spring容器中,從而實現相應的功能。代碼以下:spring
一、過濾器類api
package com.example.demo; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; public class MyFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println(servletRequest.getParameter("name")); HttpServletRequest hrequest = (HttpServletRequest)servletRequest; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse); if(hrequest.getRequestURI().indexOf("/index") != -1 || hrequest.getRequestURI().indexOf("/asd") != -1 || hrequest.getRequestURI().indexOf("/online") != -1 || hrequest.getRequestURI().indexOf("/login") != -1 ) { filterChain.doFilter(servletRequest, servletResponse); }else { wrapper.sendRedirect("/login"); } } @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } }
二、監聽器類瀏覽器
package com.example.demo; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener { public static int online = 0; @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("建立session"); online ++; } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("銷燬session"); } }
三、攔截器類安全
package com.example.demo; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; public class MyInterceptor implements HandlerInterceptor { //在請求處理以前進行調用(Controller方法調用以前 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle被調用"); Map map =(Map)httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); System.out.println(map.get("name")); System.out.println(httpServletRequest.getParameter("username")); if(map.get("name").equals("zhangsan")) { return true; //若是false,中止流程,api被攔截 }else { PrintWriter printWriter = httpServletResponse.getWriter(); printWriter.write("please login again!"); return false; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle被調用"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion被調用"); } }
四、配置類springboot
package com.example.demo; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MywebConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/zxc/foo").setViewName("foo"); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/asd/**"); } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public FilterRegistrationBean filterRegist() { FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(new MyFilter()); frBean.addUrlPatterns("/*"); System.out.println("filter"); return frBean; } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public ServletListenerRegistrationBean listenerRegist() { ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean(); srb.setListener(new MyHttpSessionListener()); System.out.println("listener"); return srb; } }
五、控制層服務器
package com.example.demo; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { private final Logger logger = LoggerFactory.getLogger(UserController.class); @Value("${application.message:Hello World}") private String message ; @GetMapping("/asd/{name}") public String welcome(@PathVariable String name,Map<String, Object> model) { model.put("time", new Date()); model.put("message", this.message); return "welcome"; } @RequestMapping("/login") @ResponseBody public Object foo() { logger.info("打印日誌----------------------"); return "login"; } @RequestMapping("/index") @ResponseBody public Object index(HttpServletRequest request) { HttpSession session = request.getSession(true); session.setAttribute("zxc", "zxc"); return "index"; } @RequestMapping("/online") @ResponseBody public Object online() { return "當前在線人數:" + MyHttpSessionListener.online + "人"; } }
六、程序主入口及application.properties文件默認。session
項目啓動後,首先測試過濾器,全部訪問路徑都需通過咱們自定義的filter,若是路徑中包含/index、/asd、/online、/login則直接經過,不然則被重定向至/login。
而後測試監聽器,先訪問http://localhost:8080/index,建立session對象(由於 session對象建立須要顯示的調用getsession方法),而後訪問http://localhost:8080/online,而後換一個瀏覽器作相同操做,以下:
最後測試攔截器,代碼展現瞭如何獲取@PathVariable註解的請求參數以及普通請求參數,首先訪問http://localhost:8080/asd/zhangsan?username=lisi,響應正常,若是訪問路徑上的名字不是zhangsan,則被攔截,例如http://localhost:8080/asd/wangwu?username=lisi,以下:
在實際項目中咱們還能夠在配置的時候設置過濾器、攔截器的執行順序及其它的參數,同時filter和listener還有對應的註解方式:@WebFilter和@WebListener,在使用註解方式時不要忘了在主程序加上@ServletComponentScan註解,這樣才能在程序啓動時將對應的bean加載進來。