SpringBoot 過(guò)濾器與攔截器實(shí)例演示
???SpringBoot中的過(guò)濾器攔截器操作與springmvc中的幾乎一樣所以這里也不過(guò)多介紹了,下面舉兩個(gè)簡(jiǎn)單的栗子演示一下
1、過(guò)濾器 ? ? ? ?
1 創(chuàng)建過(guò)濾器類(lèi)LoginFilter,實(shí)現(xiàn)servlet包下的Filter接口(包不要導(dǎo)錯(cuò)),加入注解WebFilter
package com.example.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/filter/*")
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化攔截器");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("LoginFilter");
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
//放行
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("攔截器銷(xiāo)毀");
}
}
?????2 創(chuàng)建測(cè)試類(lèi)LoginCotroller
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/filter")
public class FilterController {
@RequestMapping("/login")
public String login(){
System.out.println("登錄");
return "login";
}
}
? ? ? 3 在啟動(dòng)類(lèi)上加注解
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
//Servlet、Filter、Listener可以直接通過(guò)@WebServlet、@WebFilter、@WebListener注解自動(dòng)注冊(cè)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
? 4 啟動(dòng)測(cè)試,啟動(dòng)的時(shí)候可以看到Filter調(diào)用init方法初始化

?接著訪(fǎng)問(wèn)可以看到頁(yè)面輸出

?控制臺(tái)打印出攔截器中的語(yǔ)句

2、攔截器 ? ? ? ?
1 創(chuàng)建自定義攔截器
package com.example.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
//進(jìn)入controller方法之前調(diào)用的
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;//true表示放行 false表示不放行
}
//調(diào)用完controller之后,視圖渲染層之前
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
//頁(yè)面跳轉(zhuǎn)之后,整個(gè)流程執(zhí)行之后,一般用于資源的清理
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
? ? ? 2 創(chuàng)建攔截器配置類(lèi),注意要加上配置類(lèi)的注解
package com.example.config;
import com.example.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//設(shè)置攔截器并指定攔截路徑
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/interceptor/*");
//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");//攔截所有
//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/test");//指定不攔截
//添加自定義攔截器
WebMvcConfigurer.super.addInterceptors(registry);
}
}
? ? ? ? 3 創(chuàng)建LoginController測(cè)試類(lèi)
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/interceptor")
public class InterceptorController {
@RequestMapping("login")
public String login(){
System.out.println("Interceptor-Login");
return "login";
}
}
? ? ? 4 運(yùn)行訪(fǎng)問(wèn),查看效果

控制臺(tái)輸出如下

到此這篇關(guān)于SpringBoot 過(guò)濾器與攔截器的文章就介紹到這了,更多相關(guān)SpringBoot 過(guò)濾器與攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決
這篇文章主要介紹了SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java超詳細(xì)講解設(shè)計(jì)模式之一的工廠(chǎng)模式
工廠(chǎng)模式有 3 種不同的實(shí)現(xiàn)方式,分別是簡(jiǎn)單工廠(chǎng)模式、工廠(chǎng)方法模式和抽象工廠(chǎng)模式,工廠(chǎng)模式最大的特點(diǎn)就是解耦合,本篇帶你詳細(xì)了解它2022-03-03
Spring實(shí)戰(zhàn)之設(shè)置普通屬性值的方法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之設(shè)置普通屬性值的方法,結(jié)合實(shí)例形式分析了Spring設(shè)置普通屬性值的方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
Java concurrency之Condition條件_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Condition的作用是對(duì)鎖進(jìn)行更精確的控制。下面通過(guò)本文給大家分享Java concurrency之Condition條件的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06
spring中使用Mockito解決Bean依賴(lài)樹(shù)問(wèn)題方法
在本篇文章里小編給各位整理了關(guān)于spring中使用Mockito解決Bean依賴(lài)樹(shù)問(wèn)題方法,有需要的朋友們可以參考下。2020-01-01
解決idea中maven項(xiàng)目打包成jar報(bào)錯(cuò):沒(méi)有主清單屬性的問(wèn)題
這篇文章主要給大家分享了idea中maven項(xiàng)目打包成jar,報(bào)錯(cuò)沒(méi)有主清單屬性解決方法,文中有詳細(xì)的解決方法,如果又遇到同樣問(wèn)題的朋友可以參考一下本文2023-09-09
Java使用TCP實(shí)現(xiàn)在線(xiàn)聊天的示例代碼
這篇文章主要介紹了Java使用TCP實(shí)現(xiàn)在線(xiàn)聊天的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
java正則表達(dá)式簡(jiǎn)單使用和網(wǎng)頁(yè)爬蟲(chóng)的制作代碼
java正則表達(dá)式簡(jiǎn)單使用和網(wǎng)頁(yè)爬蟲(chóng)的制作代碼,需要的朋友可以參考一下2013-05-05
Java實(shí)現(xiàn)定時(shí)任務(wù)最簡(jiǎn)單的3種方法
幾乎在所有的項(xiàng)目中,定時(shí)任務(wù)的使用都是不可或缺的,如果使用不當(dāng)甚至?xí)斐少Y損,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時(shí)任務(wù)最簡(jiǎn)單的3種方法,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
關(guān)于Java中的頂層類(lèi)修飾問(wèn)題
這篇文章主要介紹了關(guān)于Java中的頂層類(lèi)修飾問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

