SpringBoot-(4)-Filter的使用

一,Filter

  Filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个request到达servlet之前预处理request,也可以在离开 servlet时处理response.换种说法,filter其实是一个”servlet chaining”(servlet 链).

  一个Filter包括:
  在servlet被调用之前截获;
  在servlet被调用之前检查servlet request;
  根据需要修改request头和request数据;
  根据需要修改response头和response数据;
  在servlet被调用之后截获.

 

二,SpringBoot实现Filter

  1,实现Filter接口

package com.example.demo.filters;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by zhang_guang_yang on 2018/11/21.
 */

/*
* Order表示过滤顺序,值越小,越优先过滤*/
@Order(1)
@WebFilter(filterName = "registerFilter", urlPatterns = "/register")
public class RegisterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String token = req.getHeader("token");
        String userAgent = req.getHeader("User-Agent");

        System.out.println("token: " + token);
        System.out.println("user-agent: " + userAgent);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

    @Order:过滤顺序,值越小,越优先过滤。

    @WebFilter:过滤接口配置

  2 ,添加ServletComponentScan注解

@ServletComponentScan(basePackages = {"com.example.demo.filters"} )
public class DemoApplication {

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

  @ServletComponentScan:扫描配置。

  3 ,运行效果:

  

三,也可以都在Filter中配置,不在MainApplication中添加任何注解

  

package com.example.demo.filters;

import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by zhang_guang_yang on 2018/11/21.
 */

@Component
@ServletComponentScan
@WebFilter(urlPatterns = "/register")
public class RegisterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String token = req.getHeader("token");
        String userAgent = req.getHeader("User-Agent");

        System.out.println("token: " + token);
        System.out.println("user-agent: " + userAgent);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

  

 

posted on 2018-12-02 15:45  洋子哥哥  阅读(252)  评论(0编辑  收藏  举报