Spring Boot之FilterRegistrationBean-自定义Filter
项目老的用spring写的,新的升级到了springboot,原代码中有在web.xml中定义过滤器,在boot中,自然没法这样用了,因而看了看boot如何使用自定义过滤器。
在springboot 中,主要是靠FilterRegistrationBean 这个类来提供这样的功能。
自定义 Filter
我们常常在项目中会使用 filters 用于录调用日志、排除有 XSS 威胁的字符、执行权限验证等等。Spring Boot 自动添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter,并且我们可以自定义 Filter。
两个步骤:
- 1、实现 Filter 接口,实现 Filter 方法
- 2、添加 @Configuration 注解,将自定义Filter加入过滤链
好吧,直接上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package com.zoo.lion.modules.test.test; import org.apache.catalina.filters.RemoteIpFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * @Author: xf * @Date: 2019/7/16 13:39 * @Version 1.0 */ @Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter() { return new RemoteIpFilter(); } @Bean public FilterRegistrationBean<MyFilter> testFilterRegistration() { FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>(); registration.setFilter( new MyFilter()); //过滤应用程序中所有资源,当前应用程序根下的所有文件包括多级子目录下的所有文件,注意这里*前有“/” registration.addUrlPatterns( "/*" ); //过滤指定的类型文件资源, 当前应用程序根目录下的所有html文件,注意:*.html前没有“/”,否则错误 registration.addUrlPatterns( ".html" ); //过滤指定的目录下的所有文件,当前应用程序根目录下的folder_name子目录(可以是多级子目录)下所有文件 registration.addUrlPatterns( "/folder_name/*" ); //过滤指定文件 registration.addUrlPatterns( "/index.html" ); registration.addInitParameter( "paramName" , "paramValue" ); registration.setName( "MyFilter" ); registration.setOrder( 1 ); return registration; } public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest request2 = (HttpServletRequest) request; System.out.println(request2.getRequestURI()); chain.doFilter(request, response); } @Override public void destroy() { } } } |
SpringBoot FilterRegistrationBean对象支持web的filter的排序
1.FilterRegistrationBean对象可以实现对web Filter的排序,使用setOrder(int i)方法
具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package com.dbzhang; /** * @author zdb * @description @Configuration和@Bean注解配置使用,就相当于在配置文件中配置bean的注解式用法 */ import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.catalina.filters.RemoteIpFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter(){ return new RemoteIpFilter(); } /*** * 这个FilterRegistrationBean类可以使用setOrder()方法实现对filter的排序,然后在进行注册 * https://blog.csdn.net/doctor_who2004/article/details/56055505 */ public FilterRegistrationBean testFilterRegistration(){ FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter( new MyFilter()); registration.addUrlPatterns( "/*" ); registration.addInitParameter( "paramName" , "paramValue" ); registration.setName( "MyFilter" ); registration.setOrder( 1 ); return registration; } /** * * @author zdb * @description 这个是用来实现Filter * */ public class MyFilter implements Filter{ @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; System.out.println( "this is MyFilter,url:" +request.getRequestURI()); filterChain.doFilter(servletRequest, servletResponse); } @Override public void init(FilterConfig arg0) throws ServletException { } } } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_36850813/article/details/96130013