目录
过滤器作用
注册Filter
父pom文件
pom文件
注解方式
TokenFilter
LogFilter
Logout
启动类
使用FilterRegistrationBean注册
结果
过滤器作用
过滤器用途主要包括:对用户请求进行统一认证、对用户的访问请求进行记录和审核、对用户发送的数据进行过滤或替换、转换图象格式、对响应内容进行压缩以减少传输量、对请求或响应进行加解密处理、触发资源访问事件、对XML的输出应用XSLT等。
init(FilterConfig conf)方法:用于执行过滤器的初始化工作,web容器会在web项目启动时自动调用该方法。
doFilter(ServletRequest request,SerlvetResponse response,FilterChain chain)方法:当请求和响应被过滤器拦截后,都会交给doFilter来处理:其中两个参数分别是被拦截request和response对象,可以使用chain的doFliter方法来放行。
destroy()方法:用于释放关闭Filter对象打开的资源,在web项目关闭时,由web容器自动调用该方法。
注册Filter
父pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- <version>3.1.2</version>-->
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chensir</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>8</java.version>
</properties>
<packaging>pom</packaging>
<modules>
<module>servlet</module>
</modules>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- <version>1.7.25</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chensir</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>servlet</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注解方式
TokenFilter
package com.chensir.javaWeb;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 所有api以/api开头都会走此过滤器
@WebFilter("/api/*")
@Slf4j
public class TokenFilter implements Filter {
/**
* 过滤器生成的时候调用;过滤前
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
/**
* 过滤器主要代码;过滤中
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// ServletRequest=>HttpServletRequest
HttpServletRequest request = (HttpServletRequest) servletRequest;
// ServletResponse=>HttpServletResponse
HttpServletResponse response = (HttpServletResponse) servletResponse;
log.info("-------------TokenFilter开始-------------");
// String token = request.getHeader("token");
String token = request.getParameter("token");
log.info("获取到token->{}",token);
if (StrUtil.isEmpty(token)){
response.sendRedirect("/logout");
}
StopWatch sw = new StopWatch();
sw.start();
filterChain.doFilter(servletRequest,servletResponse);
sw.stop();
log.info("-------------TokenFilter结束,耗时:{}-------------",sw.getTotalTimeSeconds());
}
/**
* 过滤器销毁的时候调用;过滤后
*/
@Override
public void destroy() {
Filter.super.destroy();
}
}
LogFilter
package com.chensir.javaWeb;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import java.io.IOException;
// 所有api以/api开头都会走此过滤器
@WebFilter("/api/*")
@Slf4j
public class LogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("-------------LogFilter开始-------------");
filterChain.doFilter(servletRequest,servletResponse);
log.info("-------------LogFilter结束-------------");
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
Logout
启动类
使用FilterRegistrationBean注册
package com.chensir.config;
import com.chensir.javaWeb.HelloServlet;
import com.chensir.javaWeb.LogFilter;
import com.chensir.javaWeb.ServletListener;
import com.chensir.javaWeb.TokenFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new HelloServlet());
servletRegistrationBean.addUrlMappings("/api/helloBaby");
return servletRegistrationBean;
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean() {
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new ServletListener());
// 启用
bean.setEnabled(true);
return bean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filter = new FilterRegistrationBean();
filter.setFilter(new TokenFilter());
filter.addUrlPatterns("/api/*");
filter.setName("Token过滤器");
// 值越小 越先执行
filter.setOrder(0);
return filter;
}
@Bean
public FilterRegistrationBean logFilterRegistrationBean(){
FilterRegistrationBean filter = new FilterRegistrationBean();
filter.setFilter(new LogFilter());
filter.addUrlPatterns("/api/*");
filter.setName("log过滤器");
// 值越小 越先执行
filter.setOrder(1);
return filter;
}
}
结果
注意:filter执行顺序正是根据注册时优先级大小运行;而监听器、过滤器、servlet的执行顺序可由下面这张图理解;