spring boot實現(xiàn)過濾器和攔截器demo
整理文檔,搜刮出一個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>>>>>>>在請求處理之前進(jìn)行調(diào)用(Controller方法調(diào)用之前)");
// 獲取系統(tǒng)時間
Calendar ca = Calendar.getInstance();
int hour = ca.get(Calendar.HOUR_OF_DAY);
// 設(shè)置限制運(yùn)行時間 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>>>>>>>請求處理之后進(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>>>>>>>在整個請求結(jié)束之后被調(diào)用,也就是在DispatcherServlet
// 渲染了對應(yīng)的視圖之后執(zhí)行(主要是用于進(jìn)行資源清理工作)");
}
}
攔截器使用: 關(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è)置限制運(yùn)行時間 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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot 過濾器、攔截器、監(jiān)聽器對比及使用場景分析
- 淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件
- Spring Boot攔截器和過濾器實例解析
- SpringBoot實現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析
- spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的方法
- 詳談springboot過濾器和攔截器的實現(xiàn)及區(qū)別
- Spring Boot使用過濾器和攔截器分別實現(xiàn)REST接口簡易安全認(rèn)證示例代碼詳解
- Spring Boot項目實戰(zhàn)之?dāng)r截器與過濾器
- SpringBoot定義過濾器、監(jiān)聽器、攔截器的方法
- Spring攔截器和過濾器的區(qū)別在哪?
相關(guān)文章
Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-03-03
SpringBoot使用Kaptcha實現(xiàn)驗證碼的生成與驗證功能
這篇文章主要介紹了SpringBoot使用Kaptcha實現(xiàn)驗證碼的生成與驗證功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及Spr
這篇文章主要介紹了淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot打包成Docker鏡像的幾種實現(xiàn)方式
Spring Boot是一個用于構(gòu)建獨立的、可執(zhí)行的Spring應(yīng)用程序的框架,結(jié)合使用Spring Boot和Docker,可以方便地將應(yīng)用程序部署到不同的環(huán)境中本文,主要介紹了SpringBoot打包成Docker鏡像的幾種實現(xiàn)方式,感興趣的可以了解一下2024-01-01
使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題
這篇文章主要介紹了使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring Boot與Spring Security的跨域問題解決方案
跨域問題是指在Web開發(fā)中,瀏覽器出于安全考慮,限制了不同域名之間的資源訪問,本文重點給大家介紹Spring Boot與Spring Security的跨域問題解決方案,感興趣的朋友一起看看吧2023-09-09

