Spring----过滤器Filter--Filter接口

这是对Filter接口源码注释的翻译以及个人的理解

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
过滤器是对资源请求(servlet或静态内容)或来自资源的响应执行过滤任务的对象,或两者兼有。

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.
过滤器在doFilter方法中执行过滤。每个Filter都可以访问FilterConfig对象,从中获取初始化参数,以及ServletContext的引用,例如,它可以使用ServletContext来加载过滤任务所需的资源。

Filters are configured in the deployment descriptor of a web
application. 过滤器在web应用程序的部署描述符中配置。

init(FilterConfig filterConfig)

Called by the web container to indicate to a filter that it is being placed into service.
由web容器调用,以指示过滤器将其放入服务中。

The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work. servlet容器在实例化过滤
器之后只调用init方法一次。init方法必须成功完成,然后才要求筛选器执行任何筛选工作。

The web container cannot place the filter into service if the init method either
如果init方法也无法使用,则web容器无法将过滤器放入服务

Throws a ServletException Does not return within a time period defined by the web container
当不在web容器定义的时间段内返回时抛出一个ServletException

FilterConfig
A filter configuration object used by a servlet container to pass information to a filter during initialization. servlet
容器使用的过滤器配置对象,在初始化期间将信息传递给过滤器。

default public void init(FilterConfig filterConfig) throws ServletException {
    
    }

doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.
每次由于客户端请求链末端的资源而通过链传递请求/响应对时,Filter的doFilter方法由容器调用。传递给此方法的FilterChain允许Filter将请求和响应传递给链中的下一个实体。

该方法的典型实现遵循以下模式:
1、检查请求 可以选择使用自定义实现包装请求对象,以过滤输入的内容或头
2、过滤
可以选择用自定义实现包装响应对象,以过滤输出的内容或头 使用FilterChain对象调用链中的下一个实体 chain.doFilter()
3、或不将请求/响应对传递给过滤器链中的下一个实体来阻塞请求处理
4、直接在过滤器链中调用下一个实体后设置响应的报头。

FilterChain用于调用下一个过滤器或资源

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException;

示例:跨域过滤器

@WebFilter(filterName = "CorsFilter ")
@Configuration
public class CorsFilter implements Filter {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(CorsFilter.class);
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    
    
        HttpServletResponse response = (HttpServletResponse) res;
        LOGGER.info("CORS_FILTER_init"+response);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
        LOGGER.info("CORS_FILTER_initEnd"+response);
        chain.doFilter(req, res);
    }
}

destroy()

alled by the web container to indicate to a filter that it is being taken out of service.
由web容器调用,以指示过滤器它正在退出服务。

This method is only called once all threads within the filter’s doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call thedoFilter method again on this instance of the filter.
只有当过滤器的doFilter方法中的所有线程都退出或超时时间过后,才会调用此方法。在web容器调用这个方法之后,它将不会在过滤器的这个实例上再次调用doFilter方法。

This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter’s current state in memory.
此方法使过滤器有机会清理所持有的任何资源(例如,内存、文件句柄、线程),并确保任何持久状态与过滤器在内存中的当前状态同步。

default public void destroy() {
    
    
    }

Guess you like

Origin blog.csdn.net/newbieJ/article/details/121373044