利用springboot如何實現(xiàn)一個過濾器功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)專注于古縣網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供古縣營銷型網(wǎng)站建設(shè),古縣網(wǎng)站制作、古縣網(wǎng)頁設(shè)計、古縣網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造古縣網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供古縣網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
攔截器定義:
@WebServlet public class ActionInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>在請求處理之前進行調(diào)用(Controller方法調(diào)用之前)"); // 獲取系統(tǒng)時間 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 設(shè)置限制運行時間 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>>>>>>>請求處理之后進行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // System.out.println(">>>MyInterceptor1>>>>>>>在整個請求結(jié)束之后被調(diào)用,也就是在DispatcherServlet // 渲染了對應(yīng)的視圖之后執(zhí)行(主要是用于進行資源清理工作)"); } }
攔截器使用: 關(guān)于注解 我使用的是@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); // 設(shè)置限制運行時間 0-4點 if (hour < 4) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setCharacterEncoding("UTF-8"); httpResponse.setContentType("application/json; charset=utf-8"); // 消息 MapmessageMap = 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); ListurlPatterns = new ArrayList (); urlPatterns.add("/service/extract/json/*"); registrationBean.setUrlPatterns(urlPatterns); return registrationBean; } }
看完上述內(nèi)容,你們掌握利用springboot如何實現(xiàn)一個過濾器功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!