spring boot實(shí)現(xiàn)過濾器和攔截器demo
整理文檔,搜刮出一個(gè)spring boot實(shí)現(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>>>>>>>在請求處理之前進(jìn)行調(diào)用(Controller方法調(diào)用之前)"); // 獲取系統(tǒng)時(shí)間 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 設(shè)置限制運(yùn)行時(shí)間 0-4點(diǎn) 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>>>>>>>請求處理之后進(jìn)行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>在整個(gè)請求結(jié)束之后被調(diào)用,也就是在DispatcherServlet // 渲染了對應(yīng)的視圖之后執(zhí)行(主要是用于進(jìn)行資源清理工作)"); } }
攔截器使用: 關(guān)于注解 我使用的是@Component 其實(shí)也可能聲明成配置
@Component public class ApplicationConfig {extends WebMvcConfigurerAdapter @Override public void addInterceptors(InterceptorRegistry registry) { // 多個(gè)攔截器組成一個(gè)攔截器鏈 // 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)時(shí)間 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 設(shè)置限制運(yùn)行時(shí)間 0-4點(diǎn) 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", "此接口可以請求時(shí)間為:0-4點(diǎn)"); 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot 過濾器、攔截器、監(jiān)聽器對比及使用場景分析
- 淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件
- Spring Boot攔截器和過濾器實(shí)例解析
- SpringBoot實(shí)現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析
- spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的方法
- 詳談springboot過濾器和攔截器的實(shí)現(xiàn)及區(qū)別
- Spring Boot使用過濾器和攔截器分別實(shí)現(xiàn)REST接口簡易安全認(rèn)證示例代碼詳解
- Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器
- SpringBoot定義過濾器、監(jiān)聽器、攔截器的方法
- Spring攔截器和過濾器的區(qū)別在哪?
相關(guān)文章
spring配置文件中util:properties和context:property-placeholder用法
這篇文章主要介紹了spring配置文件中util:properties和context:property-placeholder用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出功能的相關(guān)知識,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03Java獲取http和https協(xié)議返回的json數(shù)據(jù)
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個(gè)方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。2017-01-0118個(gè)Java8日期處理的實(shí)踐(太有用了)
這篇文章主要介紹了18個(gè)Java8日期處理的實(shí)踐(太有用了),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01mybatis?log4j2打印sql+日志實(shí)例代碼
在學(xué)習(xí)mybatis的時(shí)候,如果用log4j2來協(xié)助查看調(diào)試信息,則會大大提高學(xué)習(xí)的效率,加快debug速度,下面這篇文章主要給大家介紹了關(guān)于mybatis?log4j2打印sql+日志的相關(guān)資料,需要的朋友可以參考下2022-08-08