• SpringBoot第一章过滤器


    1、创建过滤器,注意:controller 、filter、config需要放置在application的子包下面,否则扫描不到,用于请求管道

    public class MyFilter 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 {
    
            //servletResponse.getWriter().write("777");
            System.out.println("拦截器拦截成功");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            Filter.super.destroy();
        }
    }
    

      

    2、注解方式注册过滤器

    /**
     * Servlet Filter implementation class MyFilter
    // */
    //@WebFilter(
    //        dispatcherTypes = {
    //                DispatcherType.REQUEST,
    //                DispatcherType.FORWARD,
    //                DispatcherType.INCLUDE,
    //                DispatcherType.ERROR
    //        },
    //        asyncSupported = true,
    //        description = "MyFilter器4",
    //        urlPatterns = {"/gg"},
    //        initParams = {
    //                @WebInitParam(name = "name", value = "编程帮", description = "name的描述")
    //        },
    //        servletNames = {"SuccessServlet"})
    

    3、配置类FilterConfig注册,自动扫描自动注册

    @Configuration
    public class FilterConfig {

    @Bean
    public FilterRegistrationBean heFilterRegistration() {

    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());

    filterRegistrationBean.addUrlPatterns("/*");

    return filterRegistrationBean;
    }
    }
  • 相关阅读:
    Python运算符,基本数据类型
    Python2 错误记录1File "<string>", line 1, in <module> NameError: name 'f' is not defined
    用户登录三次练习
    跟我一起学Python-day1(条件语句以及初识变量)
    vim operation
    步步为营-28-事件本质
    步步为营-27-事件
    步步为营-26-多播委托
    步步为营-25-委托(比大小)
    步步为营-24-委托
  • 原文地址:https://www.cnblogs.com/topguntopgun/p/15470926.html
Copyright © 2020-2023  润新知