SpringBoot学习3:springboot整合filter

Posted 天涯浪子心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习3:springboot整合filter相关的知识,希望对你有一定的参考价值。

整合方式一:通过注解扫描完成 Filter 组件的注册

1、编写filter

package com.bjsxt.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * Created by Administrator on 2019/2/4.
 */
@WebFilter(filterName = "FirstFilter",urlPatterns = {"/first"}) //filter名字和拦截的路径
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("进入FirstFilter.......");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("离开FirstFilter.......");
    }

    @Override
    public void destroy() {

    }
}

2、编写启动类

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * Created by Administrator on 2019/2/4.
 */
@SpringBootApplication
@ServletComponentScan   //在springboot启动时,会扫描@WebServlet、@WebFilter等,并将该类实例化
public class App {

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

 

整合方式二:通过方法完成 Filter 组件的注册

1、编写filter

package com.bjsxt.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * Created by Administrator on 2019/2/4.
 */
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("进入SecondFilter.......");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("离开SecondFilter.......");
    }

    @Override
    public void destroy() {

    }
}

2、编写启动类

package com.bjsxt;

import com.bjsxt.filter.SecondFilter;
import com.bjsxt.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * Created by Administrator on 2019/2/4.
 */
@SpringBootApplication
public class App2 {

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

    /**
     * 注册Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }

    /**
     * 注册Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        bean.addUrlPatterns(new String[]{"/second"});
        return bean;
    }
}

 

以上是关于SpringBoot学习3:springboot整合filter的主要内容,如果未能解决你的问题,请参考以下文章

springboot+springcache+shiro+Redis整合时@Cacheable@Transactional等注解失效的问题

SpringBoot学习3:springboot整合filter

Springboot学习Shiro快速入门及与SpringBoot集成

SpringBoot学习13:springboot异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)

springBoot-入门了解与学习官方文档操作

搭建 SpringBoot 项目(前端页面 + 数据库 + 实现源码)