Spring Boot 注册 Filter

in TCEHJava with 0 comment

一、简介

  关于Filter,请参阅《Java - 过滤器(Filter)与拦截器(Interceptor )区别》会有比较详细的说明。

  在SpringBoot中使用Filter主要有两种方式:注解注册代码注册

二、示例

注解注册

  1、先自定义类实现javax.servlet.Filter接口,并加上@WebFilter注解,标记Filter。
  2、在启动类(@SpringBootApplication注解标记的类)加上@ServletComponentScan注解,开启扫描服务。

具体示例如下:

package com.example.demo.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;
import javax.servlet.annotation.WebFilter;
import org.springframework.core.annotation.Order;

/**
 * 登录过滤器
 * 
 * @author Back
 *
 */
@Order(1) // Filter执行顺序。升序
@WebFilter(urlPatterns = "/*") // 过滤路径为/*,即全部过滤
public class LoginFilter implements Filter {

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

        // 可以做登录校验、字符集、日志、ip鉴权等等操作。
        chain.doFilter(request, response);

        System.out.println("结束filter");
    }

}
package com.example.demo;

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

@SpringBootApplication
@ServletComponentScan //开启扫描
public class DemoApplication {

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

代码注册

  1、先自定义类实现javax.servlet.Filter接口。
  2、在启动类(@SpringBootApplication注解标记的类)通过代码的形式注册,具体看示例。

具体示例如下:

package com.example.demo.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;

/**
 * 登录过滤器
 * 
 * @author Back
 *
 */

public class LoginFilter implements Filter {

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

        // 可以做登录校验、字符集、日志、ip鉴权等等操作。
        chain.doFilter(request, response);

        System.out.println("结束filter");
    }

}
package com.example.demo;

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.example.demo.filter.LoginFilter;

@SpringBootApplication
public class DemoApplication {

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

    @Bean
    public FilterRegistrationBean<LoginFilter> filterRegistrationBean() {
        FilterRegistrationBean<LoginFilter> registration = new FilterRegistrationBean<LoginFilter>();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(1);
        return registration;
    }
}
Comments are closed.