spring boot實現(xiàn)過濾器和攔截器demo
更新時間:2017年02月23日 16:19:09 作者:玲瓏骰子安紅豆
本篇文章主要介紹了spring boot實現(xiàn)過濾器和攔截器demo ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
整理文檔,搜刮出一個spring boot實現(xiàn)過濾器和攔截器demo ,稍微整理精簡一下做下分享。
攔截器定義:
@WebServlet public class ActionInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>在請求處理之前進行調用(Controller方法調用之前)"); // 獲取系統(tǒng)時間 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 設置限制運行時間 0-4點 if (hour < 4) { return true; } return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet // 渲染了對應的視圖之后執(zhí)行(主要是用于進行資源清理工作)"); } }
攔截器使用: 關于注解 我使用的是@Component 其實也可能聲明成配置
@Component public class ApplicationConfig {extends WebMvcConfigurerAdapter @Override public void addInterceptors(InterceptorRegistry registry) { // 多個攔截器組成一個攔截器鏈 // addPathPatterns 用于添加攔截規(guī)則 // excludePathPatterns 用戶排除攔截 registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**"); super.addInterceptors(registry); } }
過濾器:
定義:
public class ActionFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 獲取系統(tǒng)時間 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 設置限制運行時間 0-4點 if (hour < 4) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setCharacterEncoding("UTF-8"); httpResponse.setContentType("application/json; charset=utf-8"); // 消息 Map<String, Object> messageMap = new HashMap<>(); messageMap.put("status", "1"); messageMap.put("message", "此接口可以請求時間為:0-4點"); ObjectMapper objectMapper=new ObjectMapper(); String writeValueAsString = objectMapper.writeValueAsString(messageMap); response.getWriter().write(writeValueAsString); } else { chain.doFilter(request, response); } } @Override public void destroy() { } }
使用:
@Component public class ApplicationConfig { @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); ActionFilter actionFilter = new ActionFilter(); registrationBean.setFilter(actionFilter); List<String> urlPatterns = new ArrayList<String>(); urlPatterns.add("/service/extract/json/*"); registrationBean.setUrlPatterns(urlPatterns); return registrationBean; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot 過濾器、攔截器、監(jiān)聽器對比及使用場景分析
- 淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件
- Spring Boot攔截器和過濾器實例解析
- SpringBoot實現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析
- spring boot設置過濾器、監(jiān)聽器及攔截器的方法
- 詳談springboot過濾器和攔截器的實現(xiàn)及區(qū)別
- Spring Boot使用過濾器和攔截器分別實現(xiàn)REST接口簡易安全認證示例代碼詳解
- Spring Boot項目實戰(zhàn)之攔截器與過濾器
- SpringBoot定義過濾器、監(jiān)聽器、攔截器的方法
- Spring攔截器和過濾器的區(qū)別在哪?
相關文章
spring配置文件中util:properties和context:property-placeholder用法
這篇文章主要介紹了spring配置文件中util:properties和context:property-placeholder用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01SpringBoot整合EasyExcel實現(xiàn)批量導入導出
這篇文章主要為大家詳細介紹了SpringBoot整合EasyExcel實現(xiàn)批量導入導出功能的相關知識,文中的示例代碼講解詳細,需要的小伙伴可以參考下2024-03-03Java獲取http和https協(xié)議返回的json數據
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數據 ,本篇文章提供兩個方法,幫助各位如何獲取http和https返回的數據。有興趣的可以了解一下。2017-01-01