[JAVA]SpringBoot整合Filter

  1. 1.注解扫描
  2. 2.方法注册

同样有注解扫描与方法注册两种方式。

1.注解扫描

编写filter类

package xyz.xioaxin12.springboot.filter;

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

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

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("进入doFilter...");
        chain.doFilter(req, resp);
        System.out.println("退出doFilter...");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

 

拦截MyServlet

package xyz.xioaxin12.springboot.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "MyServlet",urlPatterns = "/hello")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyServlet...");
    }
}

 

启动类

package xyz.xioaxin12.springboot;

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

@SpringBootApplication
@ServletComponentScan
public class SpringBoot02FilterApplication {

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

}

 

启动运行


 

2.方法注册

1).删除原filter类上的注解。

2).将原Servlet类修改成方法注册形式。

3).修改启动类,如下

package xyz.xioaxin12.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import xyz.xioaxin12.springboot.filter.MyFilter;
import xyz.xioaxin12.springboot.servlet.MyServlet;

@SpringBootApplication
public class SpringBoot02FilterApplication {

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

    @Bean
    public ServletRegistrationBean getregistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet());
        bean.addUrlMappings("/hello");
        return bean;
    }

    @Bean
    public FilterRegistrationBean getfilterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new MyFilter());
        bean.addUrlPatterns("/hello");
        return bean;
    }

}

 

还可以拦截多个请求:

@WebFilter(filterName = "MyFilter",urlPatterns = {"*.do","*.jsp","/hello"})

 


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xiaoxin1218@qq.com

文章标题:[JAVA]SpringBoot整合Filter

文章字数:359

本文作者:周信

发布时间:2019-10-22, 22:35:41

最后更新:2019-10-22, 22:36:47

原始链接:http://zx21.xyz/2019/10/22/JAVA-SpringBoot整合Filter/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

可为我打赏鼓励~