在第一篇中进行的简单的介绍了Shiro的登录的实现https://blog.csdn.net/qq_38340127/article/details/109866392,因为主要为了后续的Spring结合Shiro,而Shiro通过Filer实现的,所以此篇主要讲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或静态内容)或来自资源的响应执行筛选任务,或同时执行这两个任务的对象)
那么也就是说
在web项目中Filer的主要功能就是对用户请求做预处理,接着将请求交给servlet处理并响应,然后Filter在对该相应进行后置处理。
web浏览器-------->web服务器--------->Filter处理-------->servlet处理--↓
web浏览器<-------web服务器<---------Filter处理<-------------------------↓
1.1 Filter使用简介
Filter的官方文档:https://tomcat.apache.org/tomcat-5.5-doc/servletapi/
//该接口主要三大功能
public interface Filter {
//初始化 实现类会继承接口中的default方法 接口也能通过default来实现方法了
default void init(FilterConfig filterConfig) throws ServletException {
}
//执行过滤
void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException;
//销毁
default void destroy() {
}
}
Method Summary | |
void |
destroy() Called by the web container to indicate to a filter that it is being t |