SpringBoot中FilterRegistrationBean对象支持web的filter的排序

1.FilterRegistrationBean对象可以实现对web Filter的排序,使用setOrder(int i)方法,具体代码如下:

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/doctor_who2004/article/details/56055505

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值