标签:pos scan boot 执行 turn his oid 使用 zed
首先在介绍springboot实现过滤器之前,我们先来了解下sevlet的三大组件:
Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter、@WebListener
Servlet 三大组件 Servlet、Filter、Listener 在传统项目中需要在 web.xml 中进行相应的配置。Servlet 3.0 开始在 javax.servlet.annotation 包下提供 3 个对应的 @WebServlet、@WebFilter、@WebListener 注解来简化操作
@WebServlet、@WebFilter、@WebListener 写在对应的 Servlet、Filter、Listener 类上作为标识,从而不需要在 web.xml 中进行配置了
Spring Boot 应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 标准的 Servlet ,实现 javax.servlet.http.HttpServlet. 重写其 doGet 、doPost 方法
* name :表示 servlet 名称,可以不写,默认为空
* urlPatterns: 表示请求的路径,如 http://ip:port/context-path/userServlet
*/
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"})
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuffer requestURL = req.getRequestURL();
System.out.println("com.wmx.servlet.UserServlet -- " + requestURL);
resp.sendRedirect("/index.html");//浏览器重定向到服务器下的 index.html 页面
}
}
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 标准 Servlet 过滤器,实现 javax.servlet.Filter 接口,并重现它的 3 个方法
* filterName:表示过滤器名称,可以不写
* value:配置请求过滤的规则,如 "/*" 表示过滤所有请求,包括静态资源,如 "/user/*" 表示 /user 开头的所有请求
*/
@WebFilter(filterName = "SystemFilter", value = {"/*"})
public class SystemFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("com.wmx.servlet.SystemFilter -- 系统启动...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//转为 HttpServletRequest 输出请求路径
HttpServletRequest request = (HttpServletRequest) servletRequest;
System.out.println("com.wmx.servlet.SystemFilter -- 过滤器放行前...." + request.getRequestURL());
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("com.wmx.servlet.SystemFilter -- 过滤器返回后...." + request.getRequestURL());
}
@Override
public void destroy() {
System.out.println("com.wmx.servlet.SystemFilter -- 系统关闭...");
}
}
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 标准 Servlet 监听器,实现 javax.servlet.ServletContextListener 接口,并重写方法
* ServletContextListener 属于 Servlet 应用启动关闭监听器,监听容器初始化与销毁。常用的监听器还有:
* ServletRequestListener:HttpServletRequest 对象的创建和销毁监听器
* HttpSessionListener:HttpSession 数据对象创建和销毁监听器
* HttpSessionAttributeListener 监听HttpSession中属性变化
* ServletRequestAttributeListener 监听ServletRequest中属性变化
*/
@WebListener
public class SystemListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("com.wmx.servlet.SystemListener -- 服务器启动.");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("com.wmx.servlet.SystemListener -- 服务器关闭.");
}
}
Spring Boot 应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //对 servlet 注解进行扫描
public class RedisStuWebApplication {
public static void main(String[] args) {
SpringApplication.run(RedisStuWebApplication.class, args);
}
}
在介绍完sevlet的三大组件之后,我们再来学习如何实现过滤器就不会那么迷茫了……
使用@WebFilter注解为声明当前类为filter,第一个参数为该filter起一个名字,第二个参数为说明要拦截的请求地址,当前类需要实现Filter接口,里面有三个方法,分别为过滤器初始化、过滤方法和过滤器销毁
@Slf4j
@WebFilter(filterName = "myFilter1", urlPatterns = "/*")
public class MyFilter1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info(filterConfig.getFilterName() + " init");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
log.info("myFilter1 begin");
try {
log.info("业务方法执行");
chain.doFilter(request, response);
} catch (Exception e) {
log.error("error!", e);
}
log.info("myFilter1 end");
}
@Override
public void destroy() {
}
}
启动类添加@ServletComponentScan注解,@ServletComponentScan注解所扫描的包路径必须包含该Filter,代码如下:
之所以加这个注解,因为springboot默认不扫描Servlet的三大组件,所以需要加
@SpringBootApplication
@ServletComponentScan(basePackages = "com.example.demo.filter")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
新建MyFilter2.java类,不要加注解@WebFilter,代码如下:
@Slf4j
public class MyFilter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info(filterConfig.getFilterName() + " init");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
log.info("myFilter2 begin");
try {
log.info("业务方法执行");
chain.doFilter(request, response);
} catch (Exception e) {
log.error("error!", e);
}
log.info("myFilter2 end");
}
@Override
public void destroy() {
}
}
新建配置类WebConfig.java,配置bean,代码如下:
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean testFilterRegistration() {
//配置多个过滤规则
// List<String> urls = new ArrayList<>();
// urls.add("/order/*");
// urls.add("/user/*");
// registration.setUrlPatterns(urls);
FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter2());
registration.addUrlPatterns("/test"); //
registration.setName("myFilter2");
return registration;
}
}
项目启动后浏览器访问http://localhost:8080/test,可以看到过滤器已生效,后台打印日志如下:
[nio-8080-exec-1] com.example.demo.filter.MyFilter1 : myFilter1 begin
[nio-8080-exec-1] com.example.demo.filter.MyFilter1 : 业务方法执行
[nio-8080-exec-1] com.example.demo.filter.MyFilter1 : myFilter1 end
到此Spring Boot配置过滤器的两种方式已经全部实现,有问题欢迎留言沟通哦!
标签:pos scan boot 执行 turn his oid 使用 zed
原文地址:https://www.cnblogs.com/fangh816/p/13324527.html