设计网站公司湖南岚鸿公司,机械设备上哪个网站做外贸推广,仿一个网站,天眼查企业查询系统官网Filter#xff0c;过滤器#xff0c;属于Servlet规范#xff0c;并不是Spring独有的。其作用从命名上也可以看出一二#xff0c;拦截一个请求#xff0c;做一些业务逻辑操作#xff0c;然后可以决定请求是否可以继续往下分发#xff0c;落到其他的Filter或者对应的Servl…Filter过滤器属于Servlet规范并不是Spring独有的。其作用从命名上也可以看出一二拦截一个请求做一些业务逻辑操作然后可以决定请求是否可以继续往下分发落到其他的Filter或者对应的Servlet 简单描述下一个http请求过来之后一个Filter的工作流程
首先进入filter执行相关业务逻辑若判定通行则进入Servlet逻辑Servlet执行完毕之后又返回Filter最后在返回给请求方判定失败直接返回不需要将请求发给Servlet 这次测试用到的依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion3.0.7/version
/dependency!-- MySQL Connector --
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.11/version
/dependency!-- Lombok --
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.32/versionscopeprovided/scope
/dependency!-- MyBatis-Plus --
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-spring-boot3-starter/artifactIdversion3.5.5/version
/dependency!-- Spring Boot Starter JDBC --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactIdversion3.3.0/version
/dependency!-- HikariCP --
dependencygroupIdcom.zaxxer/groupIdartifactIdHikariCP/artifactIdversion5.0.0/version
/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.1/versionscopecompile/scope
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversionRELEASE/versionscopecompile/scope
/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion6.0.9/version
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-test/artifactIdversion3.0.7/version
/dependency!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --
dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.47/version
/dependency/dependencies
在Spring中如果需要定义一个过滤器直接实现Filter接口即可
package com.hayaizo.transactional.filter;import com.alibaba.fastjson.JSON;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;import java.io.IOException;Slf4j
WebFilter
public class ReqFilter implements Filter {public ReqFilter() {System.out.println(init reqFilter);}Overridepublic void init(FilterConfig filterConfig) throws ServletException {}Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {HttpServletRequest req (HttpServletRequest) request;log.info(url{}, params{}, req.getRequestURI(), JSON.toJSONString(req.getParameterMap()));chain.doFilter(req, response);}Overridepublic void destroy() {}
}
实现一个自定义的Filter容易一般有两个步骤
实现 Filter 接口在doFilter方法中添加业务逻辑如果允许访问继续则执行chain.doFilter(req, response); 不执行上面这一句则访问到此为止
接下来的一个问题就是如何让我们自定义的Filter生效在SpringBoot项目中有两种常见的使用方式
WebFilter包装Bean: FilterRegistrationBean
那么如何让WebFilter生效呢 在Spring的启动类上添加ServletComponentScan注解即可。 WebFilter常用属性如下其中urlPatterns最为常用表示这个filter适用于哪些url请求默认场景下全部请求都被拦截 WebFilter注解是Java Servlet规范中用于定义过滤器的注解。它有多个属性每个属性都有特定的类型和用途。以下是WebFilter注解的属性、类型和用途的表格
属性名类型用途filterNameString指定过滤器的名称。urlPatternsString[]指定过滤器的URL模式可以是多个。valueString[]等同于urlPatterns是一个快捷方式不能同时使用urlPatterns和value。servletNamesString[]指定过滤器应用到的Servlet名称。dispatcherTypesDispatcherType[]指定过滤器应用到的调度类型如REQUEST, FORWARD, INCLUDE, ERROR, ASYNC。initParamsWebInitParam[]指定过滤器的初始化参数。asyncSupportedboolean指定过滤器是否支持异步操作默认为false。descriptionString过滤器的描述信息。displayNameString过滤器的显示名称。smallIconString过滤器的小图标。largeIconString过滤器的大图标。
下面给出一个具体例子来更好的理解这些注解的使用方法和含义
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;// 定义一个过滤器使用WebFilter注解
WebFilter(filterName ExampleFilter, urlPatterns {/example/*}, dispatcherTypes {DispatcherType.REQUEST, DispatcherType.FORWARD}, initParams {WebInitParam(name param1, value value1),WebInitParam(name param2, value value2)},asyncSupported true,description This is an example filter,displayName Example Filter
)
public class ExampleFilter implements Filter {Overridepublic void init(FilterConfig filterConfig) throws ServletException {// 获取初始化参数String param1 filterConfig.getInitParameter(param1);String param2 filterConfig.getInitParameter(param2);System.out.println(Filter initialized with parameters: param1 , param2);}Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {// 在请求处理之前执行的代码System.out.println(ExampleFilter is filtering the request...);// 继续执行请求chain.doFilter(request, response);// 在响应返回给客户端之前执行的代码System.out.println(ExampleFilter has filtered the response...);}Overridepublic void destroy() {// 清理资源System.out.println(ExampleFilter is being destroyed...);}
}filterName 指定了过滤器的名称为 “ExampleFilter”。urlPatterns 指定了过滤器应用于以 “/example/” 开头的URL。dispatcherTypes 指定了过滤器应用于 REQUEST 和 FORWARD 类型的请求调度。initParams 指定了两个初始化参数 “param1” 和 “param2” 及其对应的值。asyncSupported 指定过滤器支持异步操作。description 提供了过滤器的描述信息。displayName 指定了过滤器的显示名称。
对于执行顺序 结论对于被Spring管理的Filter过滤器可以使用Order注解来设置执行顺序 也可以使用FilterRegistrationBean来包装自定义的Filter
Beanpublic FilterRegistrationBeanReqFilter filterRegistrationBean(ReqFilter reqFilter) {FilterRegistrationBeanReqFilter filterRegistrationBean new FilterRegistrationBean();filterRegistrationBean.setFilter(reqFilter);filterRegistrationBean.addUrlPatterns(/*);filterRegistrationBean.setOrder(2147483647);return filterRegistrationBean;}此外格外注意, WebFilter声明的Filter优先级为2147483647(最低优先级)