spring boot (六) 使用servletAPI-自定义过滤器,拦截器

方式一

直接在springboot启动类上加上servlet扫描

@ServletComponentScan
Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
启动类:
@ServletComponentScan
@SpringBootApplication
public class BootServletApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootServletApplication.class, args);
    }

}

  Filter:

@WebFilter(filterName = "MyFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("执行myfilter");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

  Listener:

@WebListener
public class MyListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public MyListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        System.out.println("servlet容器启动");
    }

  servlet

@WebServlet(name = "MyServlet",urlPatterns = "/my")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行servlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

  方式二

 

使用servletapi,xxxRegistrationBean-自定义

@SpringBootConfiguration
public class ServletConfiguration {
    @Bean
    public ServletRegistrationBean<MyServlet> registrationBean(){
        ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(new MyServlet(),"/my");

        return bean;
    }

    @Bean
    public FilterRegistrationBean<MyFilter> filterFilterRegistrationBean(){
        FilterRegistrationBean<MyFilter> file = new FilterRegistrationBean<>();
        file.setFilter(new MyFilter());
        file.setUrlPatterns(Arrays.asList("/"));
        return file;
    }
    @Bean
    public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean(){
        ServletListenerRegistrationBean<MyListener> listener =  new ServletListenerRegistrationBean<>();
        listener.setListener(new MyListener());
        return listener;
    }

}

  拦截器:

/**
 * 拦截器的配置类
 */
@SpringBootConfiguration
public class MyWebMVC extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Myinterceptor());
    }
}
/**
 * 拦截器
 */
public class Myinterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("posthandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterHandle");
    }
}

  

 

posted @ 2019-05-06 22:19  AlexZS  Views(327)  Comments(0)    收藏  举报