• Spring Boot 整合Filter


    整合Filter 方式一
    3.1通过注解扫描完成Filter 组件注册
    3.1.1创建Filter

    /**
    * 整合Filter 方式一
    */
    //@WebFilter(filterName = "FirstFilter",urlPatterns =
    {"*.do","*.jsp"})
    @WebFilter(filterName = "FirstFilter",urlPatterns =
    "/first")
    public class FirstFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws
    ServletException {
    } @Override
    public void doFilter(ServletRequest servletRequest,

    ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
    System.out.println("进入First Filter");
    filterChain.doFilter(servletRequest,servletResponse);
    System.out.println("离开First Filter");
    } @
    Override
    public void destroy() {
    }
    }

    3.1.2修改启动类
    @SpringBootApplication
    @ServletComponentScan//在spring Boot 启动时会扫描
    @WebServlet,@WebFilter 注解,并将该类实例化
    public class SpringbootwebApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringbootwebApplication.class,
    args);
    }
    }

    * 整合Filter 方式二
    */
    public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws
    ServletException {
    } @
    Override
    public void doFilter(ServletRequest servletRequest,
    ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
    System.out.println("进入Second Filter");
    filterChain.doFilter(servletRequest,servletResponse);
    System.out.println("离开Second Filter");
    }
    @Override
    public void destroy() {
    }
    }

    4.1.2创建Filter 配置类
    /**
    * Filter 配置类
    */
    @Configuration
    public class FilterConfig {
    @Bean
    public FilterRegistrationBean
    getFilterRegistrationBean(){
    FilterRegistrationBean bean = new
    FilterRegistrationBean(new SecondFilter());

    // bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
    bean.addUrlPatterns("/second");
    return bean;
    }
    }

    作者:郑金圣
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    this.$route和this.$router的区别
    IE不支持 Promise 解决办法
    滚动定位的多种方法
    css设置禁止文字被选中
    input标签内容改变的触发事件
    webpack-dev-server配置指南webpack3.0
    Object.keys方法之详解
    mac安装webpack失败
    vue2使用animate css
    http
  • 原文地址:https://www.cnblogs.com/zhengjinsheng/p/12851268.html
Copyright © 2020-2023  润新知