前情提要
web開發使用Controller基本能解決大部分的需求,但是有時候我們也需要使用Filter,因爲相對於攔截和監聽來說,有時候原生的還是比較好用的,現在就來簡單的在SpringBoot中使用這些特殊類吧
好吧,上面這句話是複製粘貼前面的使用Servlet文章的前情提要,懶得寫了,直接進入正題吧
使用Filter實例
在SpringBoot中使用Filter也有兩種方式:註解註冊Filter和代碼註冊,同樣也分別進行舉例:
通過註解的方式進行註冊:
首先,創建一個Filter,並使用WebFilter註解進行修飾,表示該類是一個Filter,以便於啓動類進行掃描的時候確認,代碼如下:
package org.framework.demo.section1;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 在SpringBoot中通過註解註冊的方式簡單的使用Filter
* @author chengxi
*/
@WebFilter(urlPatterns = "/*", filterName = "myfilter")
public class FileterController implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter初始化中");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("開始進行過濾處理");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("Filter銷燬中");
}
}
然後創建一個啓動類,該啓動類中同樣額外添加一個註解用於自動掃描指定包下(默認是與啓動類同包下)的WebFilter/WebServlet/WebListener等特殊類,代碼如下:
package org.springframework.demo.section;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* 啓動類測試
* @author chengxi
*/
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args){
SpringApplication.run(Main.class, args);
}
}
然後啓動該類,輸入網址:localhost:8080/index
,不管我們是否寫了該url請求的處理方法,我們查看控制檯輸出將會發現,Filter已經被初始化並且被使用了
接着,我們來通過代碼註冊的方式來使用Filter:
首先,創建一個Filter類,該類不使用WebFilter進行修飾:
package org.framework.demo.section1;
import javax.servlet.*;
import java.io.IOException;
/**
* 在SpringBoot中簡單使用Filter
* @author chengxi
*/
public class FileterController implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter初始化中");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("開始進行過濾處理");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("Filter銷燬中");
}
}
然後,編寫啓動類,這裏的啓動類就不能直接使用註解ServletComponentScan來自動掃描了,需要我們手動添加代碼來進行註冊,需要用到的註冊類是:FilterRegistrationBean,代碼方式註冊Filter代碼如下:
package org.springframework.demo.section;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
/**
* 啓動類測試
* @author chengxi
*/
@SpringBootApplication
public class Main {
/**
* 代碼方式註冊Bean
* @return
*/
@Bean
public FilterRegistrationBean setFilter(){
FilterRegistrationBean filterBean = new FilterRegistrationBean();
filterBean.setFilter(new FilterController());
filterBean.setName("FilterController");
filterBean.addUrlPatterns("/*");
return filterBean;
}
public static void main(String[] args){
SpringApplication.run(Main.class, args);
}
}
然後,我們啓動該類,並輸入網址:localhost:8080/index
,同樣打開控制檯查看輸出日誌將會發現,該Filter也生效了