• Spring Boot 整合Filter


    两种方法

    方法一:

      正常创建好Filter类,配置完成

    package clc.user.filter;
    
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    /**
     * ClassName: FirstFilter<br/>
     * Description: <br/>
     * date: 2019/4/1 5:31 PM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    @WebFilter(filterName = "FirstFilter", urlPatterns = {"/FirstFilter", "/FirstServlet"})
    public class FirstFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("init-FirstFilter");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("doFilter-FirstFilter");
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy-FirstFilter");
        }
    }

    启动类使用注解ServletComponentScan

    @SpringBootApplication
    @ServletComponentScan
    public class UserServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class, args);
        }
    }

    方法二:

    写好Filte但不进行配置,在启动类中进行配置

    package clc.user.filter;
    
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    /**
     * ClassName: SecondFilter<br/>
     * Description: <br/>
     * date: 2019/4/1 5:31 PM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    public class SecondFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("init-SecondFilter");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("doFilter-SecondFilter");
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy-SecondFilter");
        }
    }
    @SpringBootApplication
    public class UserServiceApplication2 {
    
        @Bean
        public ServletRegistrationBean getServletRegistrationBean() {
            ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
            bean.addUrlMappings("/SecondServlet");
            return bean;
        }
    
    
        @Bean
        public FilterRegistrationBean getFilterRegistrationBean() {
            FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
            bean.addUrlPatterns("/SecondServlet","/123");
            return bean;
        }
    
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication2.class, args);
        }
    }

    注意,所有的路径必须以"/"开头

  • 相关阅读:
    老版本的Spring应用该如何应对CVE202222965漏洞?
    Spring官宣网传大漏洞,并提供解决方案
    IDEA 2022.2.1 Beta 2发布:新增支持Java 18、增强JUnit 5的支持
    这大概是“毕业”式裁员的祖宗?
    IDEA小技巧:Debug条件断点
    Java 8 + 10 = Java 18
    这些OAuth2客户端的认证方式你未必了解
    Spring Security 一键接入验证码登录和小程序登录
    Java的虚拟线程(协程)特性开启预览阶段,多线程开发的难度将大大降低
    Java 9 的模块(Module)系统
  • 原文地址:https://www.cnblogs.com/chenglc/p/10637809.html
Copyright © 2020-2023  润新知