SpringBoot 之Filter 常用配置方式

2021-09-27 06:53:33 字數 1752 閱讀 4994

springboot 配置filter過濾器方式一:

@webfilter(filtername = "urlfilter", urlpatterns = "/*")

public class urlfilter implements filter

@override

public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception else

}@override

public void destroy()

}

注意:@webfilter註解為宣告此類為filter,第乙個引數為該filter起乙個名字,第二個引數為說明要攔截的請求位址。該類實現了filter介面,裡面有三個方法,分別為過濾器建立、過濾方法和過濾器銷毀。

main函式類上新增@servletcomponentscan(basepackages = "此處寫明filter位址,格式為包名+類名(如com.digipowert.ucas.filter)")

package com.digipower;

import org.springframework.boot.web.servlet.servletcomponentscan;

import org.springframework.boot.web.support.springbootservletinitializer;

@servletcomponentscan("com.digipowert.ucas.filter")

public static void main(string args)

@override

}}

public class urlfilter implements filter 

@override

public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception else

}@override

public void destroy()

}

package com.digipower;

import org.springframework.boot.web.servlet.filterregistrationbean;

import org.springframework.boot.web.servlet.servletcomponentscan;

import org.springframework.boot.web.support.springbootservletinitializer;

import org.springframework.context.annotation.bean;

public static void main(string args)

@override

}@bean

public filterregistrationbean urlfilterregistration()

}

Springboot自定義過濾器Filter

前言 自己寫了個springboot專案,最近寫的功能越來越多,結合業務已經要寫過濾器filter來過濾處理一些請求。在網上看了幾篇部落格,總結如下 過濾器配置方式有兩種 1 通過 webfilter註解來配置 2 通過 bean註解來配置 第一步 首先自己先寫個過濾器類 myfilter,此類必須...

python3學習之map函式和filter函式

map 函式可以對乙個資料進行同等迭代操作。例如 def f x return x x r map f,1,2,3,4,5,6,7,8,9 print list r map函式傳入的第乙個引數就是函式本身,即f。第二個引數是要操作的資料 map 作為高階函式,事實上它把運算規則抽象了,因此,我們不 ...

lucene中search內建的Filter

先來看一下rangefilter public static void printfilter string path throws ioexception system.out.println 其實這段 沒什麼,就增加了下面這段 rangefilter filter new rangefilter...