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

