SpringBoot整合Filter

2021-03-10 17:35

阅读:472

标签:color   app   gen   conf   fir   leave   pat   get   exce   

SpringBoot整合Filter有两种方式:

方式一:通过扫描注解方式完成Filter组件注册

步骤一:创建一个Filter

package com.shiqp.test.filter;

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.annotation.WebFilter;

/**
 * 
 * @description SpringBoot整合Filter
 * @author shiqi
 * @date 2020-12-15下午8:42:24
 */
@WebFilter(filterName = "FirstFilter", urlPatterns = "/test")
public class FirstFilter implements Filter {
    /**
     * @desc 初始化
     */
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        System.out.println("enter filter...");
        chain.doFilter(request, response);
        System.out.println("leave filter...");

    }

    /**
     * @desc 销毁
     */
    public void destroy() {

    }

}

步骤二:编写一个启动类

package com.shiqp.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class TestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}

}

步骤三:启动TestApplication

步骤四:在浏览器中输入访问地址http://localhost:8080/test

步骤五:查看访问日志

技术图片

 

 至此,方式一结束

方式二:通过方法完成Filter组件注册

步骤一:创建一个Filter

package com.shiqp.test.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SecondFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("enter filter...");
		chain.doFilter(request, response);
		System.out.println("leave filter...");
	}

}

 步骤二:创建一个启动类

package com.shiqp.test;

import javax.servlet.Filter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.shiqp.test.filter.SecondFilter;

@SpringBootApplication
public class TestApplication1 {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication1.class, args);
	}

	/**
	 * 
	 * @description 注册filter类
	 * @author shiqi
	 * @date 2020-12-15 20:54:37
	 * @return
	 */
	@Bean
	public FilterRegistrationBean getFilterRegistrationBean() {
		FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
		bean.addUrlPatterns("/test");
		return bean;
	}

}

其余步骤同方式一。

 

回顾一下,SpringMVC配置过滤器:

    filter>
        filter-name>过滤器名filter-name>
        filter-class>过滤器路径filter-class>
    filter>
    filter-mapping>
        filter-name>过滤器名filter-name>
        url-pattern>过滤规则url-pattern>
    filter-mapping>

由上可见,SpringBoot通过注解或方法的方式实现了SpringMVC的Filter节点配置。非常的方便。

开发环境:

JDK1.8

SpringBoot2.1.6.RELEASE

SpringBoot整合Filter

标签:color   app   gen   conf   fir   leave   pat   get   exce   

原文地址:https://www.cnblogs.com/babylove/p/14141100.html


评论


亲,登录后才可以留言!