码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot: 3.SpringBoot整合filter(转)

时间:2019-02-24 18:55:49      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:oid   boot   vax   tco   stat   post   att   web   nis   

整合方式一:通过注解扫描完成 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(转)

标签:oid   boot   vax   tco   stat   post   att   web   nis   

原文地址:https://www.cnblogs.com/kuangzhisen/p/10427125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!