springboot 經過@WebFilter(urlPatterns )配置Filter過濾路徑

springboot 經過@WebFilter(urlPatterns )配置Filter過濾路徑,沒有配置/*,輸入任何路徑都能進過濾器 2019年04月25日 12:51:33 peigui.huang 閱讀數 1005 版權聲明:本文爲博主原創文章,遵循 CC 4.0 by-sa 版權協議,轉載請附上原文出處連接和本聲明。 本文連接:https://blog.csdn.net/huangpeigui/article/details/89513769 @Slf4j @Component @ServletComponentScan @WebFilter(urlPatterns = {"/config/*","/driver/*","/order/*","/im/*","/privacy/*","/config/*"}, filterName = "apiFilter") public class SecurityRequestFilter implements Filter { } 以上代碼,urlPatterns 沒有指名要過濾「/」根路徑,可是在輸入http://localhost:8080/以後,卻能進入filter。 啓動打印日誌以下: 觀察日誌能夠看出,註冊的過濾器除了使用filterName = "apiFilter"顯示註冊的外,還隱試註冊了一個以類名首字母爲小寫的過濾器(securityRequestFilter ) 解決方法:將 filterName = "apiFilter" 修改成 filterName = "securityRequestFilter",覆蓋掉隱試註冊的過濾器,這樣就能夠避免註冊多個過濾器。從而解決輸入任何路徑都能進過濾器的問題。

__________________________________________________________________________html

 

https://www.jianshu.com/p/05c8be17c80ajava

前言

以往的javaEE增長Filter是在web.xml中配置,然而spring-boot中很明顯不能這樣實現,那怎麼辦呢?看完下面的教程,答案天然知道了。git

前言

傳統的javaEE增長Filter是在web.xml中配置,如如下代碼:github

<filter> <filter-name>TestFilter</filter-name> <filter-class>com.cppba.filter.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/*</url-pattern> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </filter-mapping> 

然而spring-boot中很明顯不能這樣實現,那怎麼辦呢?看完下面的教程,答案天然知道了。web


老方法(新方法請直接下拉)

1.建立自定義Filter

package com.cppba.filter; import javax.servlet.*; import java.io.IOException; public class TestFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("TestFilter"); } @Override public void destroy() { } } 

2.在ApplicationConfiguration.java中增長一個@bean

@Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new TestFilter()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("testFilter"); registration.setOrder(1); return registration; } 

3.啓動項目

你會看到控制檯打印以下代碼:spring


 
https://github.com/bigbeef/cppba-spring-boot

4.訪問項目

最後咱們訪問如下http://127.0.0.1:8080/test
若是你看到控制檯打印出:TestFilter
api

 
https://github.com/bigbeef/cppba-spring-boot

恭喜你,配置成功!

 


2017-04-20 最新spring-boot增長Filter方法

首先定義一個Filterspringboot

@Order(1) //重點 @WebFilter(filterName = "testFilter1", urlPatterns = "/*") public class TestFilterFirst implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("TestFilter1"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } } 

比較核心的代碼是自定義類上面加上@WebFilter,其中@Order註解表示執行過濾順序,值越小,越先執行app

咱們在spring-boot的入口處加上以下註解@ServletComponentScan:異步

@SpringBootApplication(scanBasePackages = "com.cppba") //重點 @ServletComponentScan public class Application { public static void main(String[] args) throws UnknownHostException { SpringApplication app = new SpringApplication(Application.class); Environment environment = app.run(args).getEnvironment(); } } 

這種方法效果和上面版本同樣,可是用起來更加方便!


 ____________________________________________________________________________________

Web 過濾器參數設置問題

Posted on  2017-09-06 22:00 耍流氓的兔兔 閱讀(197) 評論(0) 編輯 收藏

問題描述:

複製代碼
在代碼定義了3個過濾器,分別爲filter1,filter2,filter3,過濾的Servlet範圍分別是"/*","/Servlet1","/Servlet1",只在filter3種配置了初始化參數

預設結果爲:

    filter1...進...
    filter2...進...
    filter3...進...
    com.roxy_filter.Filter3
    hello
    filter3...出...
    filter2...出...
    filter1...出...

 

  運行結果倒是:

 

    filter1...進...
    filter2...進...
    filter2...出...
    filter1...出...

複製代碼

問題代碼:

複製代碼
@WebFilter(filterName="filter3", servletNames="/Servlet1",   
        initParams={  
                @WebInitParam(name="ok", value="hello")
    }  
) 


public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

  System.out.println("filter3...進...");


  System.out.println(Thread.currentThread().getStackTrace()[1].getClassName());
  String v = fConfig.getInitParameter("ok");
  System.out.println(v);

 
    

  chain.doFilter(request, response);

  System.out.println("filter3...出...");
}

 
    
複製代碼

問題分析:

首先將filter3的@WebFilter聲明改成和filter2一致,輸出正常,說明doFilter()方法沒有問題

仔細查看文檔,發現有兩個參數:
  servletNames String[]:指定過濾器將應用於哪些 Servlet。取值是 @WebServlet 中的 name 屬性的取值,或者是 web.xml 中 <servlet-name> 的取值
  urlPatterns :指定要過濾的URL模式,也可以使用屬性value來聲明.(指定要過濾的URL模式是必選屬性)
因此將servletNames="/Servlet1"改成urlPatterns ="/Servlet1",代表只對Servlet1進行過濾,運行,結果正確
 
   

 

問題解決:

@WebFilter(filterName="filter3", urlPattens="/Servlet1",   
        initParams={  
                @WebInitParam(name="ok", value="hello")
    }  
)

 

問題總結:

複製代碼
web3.0 以後,對於servlet,filter,listener有兩種配置方式,一種是在web.xml種進行傳統的配置,另外一種是直接在類種進行註解式聲明

@WebFilter 用於將一個類聲明爲過濾器,該註解將會在部署時被容器處理,容器將根據具體的屬性配置將相應的類部署爲過濾器。該註解具備下表給出的一些經常使用屬性 ( 如下全部屬性均爲可選屬性,可是 value、urlPatterns、servletNames 三者必需至少包含一個,且 value 和 urlPatterns 不能共存,若是同時指定,一般忽略 value 的取值 )

屬性名 類型 描述
filterName String 指定過濾器的 name 屬性,等價於 <filter-name>
value String[] 該屬性等價於 urlPatterns 屬性。可是二者不該該同時使用
urlPatterns String[] 指定一組過濾器的 URL 匹配模式。等價於 <url-pattern> 標籤
servletNames String[] 指定過濾器將應用於哪些 Servlet。取值是 @WebServlet 中的 name 屬性的取值,或者是 web.xml 中 <servlet-name> 的取值
dispatcherTypes DispatcherType 指定過濾器的轉發模式。具體取值包括:
ASYNC、ERROR、FORWARD、INCLUDE、REQUEST
initParams WebInitParam[] 指定一組過濾器初始化參數,等價於 <init-param> 標籤
asyncSupported boolean 聲明過濾器是否支持異步操做模式,等價於 <async-supported> 標籤
description String 該過濾器的描述信息,等價於 <description> 標籤
displayName String 過濾器的顯示名,一般配合工具使用,等價於 <display-name> 標籤
複製代碼
相關文章
相關標籤/搜索