SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战

补充:SpringBoot启动日志

1、深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识)
  简介:讲解SpringBoot里面Filter讲解和使用Servlet3.0配置自定义Filter实战

  filter简单理解:人--->检票员(filter)---> 景点

  1、SpringBoot启动默认加载的Filter
    characterEncodingFilter
    hiddenHttpMethodFilter
    httpPutFormContentFilter
    requestContextFilter

  2、Filter优先级

    Ordered.HIGHEST_PRECEDENCE
    Ordered.LOWEST_PRECEDENCE

    低位值意味着更高的优先级 Higher values are interpreted as lower priority
    自定义Filter,避免和默认的Filter优先级一样,不然会冲突

    注册Filter的bean FilterRegistrationBean
    同模块里面有相关默认Filter
    web->servlet->filter


  3、自定义Filter
    1)使用Servlet3.0的注解进行配置
    2)启动类里面增加 @ServletComponentScan,进行扫描
    3)新建一个Filter类,implements Filter,并实现对应的接口
    4) @WebFilter 标记一个类为filter,被spring进行扫描
      urlPatterns:拦截规则,支持正则

    5)控制chain.doFilter的方法的调用,来实现是否通过放行
    不放行,web应用resp.sendRedirect("/index.html");
    场景:权限控制、用户登录(非前端后端分离场景)等

    1、官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners

   代码示例:

  启动类中加入@ServletComponentScan注解

  

  LoginFilter.java:

 1 package net.xdclass.demo.Filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.annotation.WebFilter;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 
16 @WebFilter(urlPatterns = "/api/*", filterName = "loginFilter")
17 public class LoginFilter implements Filter{
18     
19     
20     
21      /**
22       * 容器加载的时候调用
23       */
24       @Override
25       public void init(FilterConfig filterConfig) throws ServletException {
26           System.out.println("init loginFilter");
27       }
28 
29       
30       /**
31        * 请求被拦截的时候进行调用
32        */
33       @Override
34       public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
35           System.out.println("doFilter loginFilter");
36           
37           HttpServletRequest req = (HttpServletRequest) servletRequest;
38           HttpServletResponse resp = (HttpServletResponse) servletResponse;
39           String username = req.getParameter("username");
40           
41           if ("xdclass".equals(username)) {
42               filterChain.doFilter(servletRequest,servletResponse);
43           } else {
44               resp.sendRedirect("/index.html");
45               return;
46           }
47           
48           
49 
50       }
51 
52       /**
53        * 容器被销毁的时候被调用
54        */
55       @Override
56       public void destroy() {
57           System.out.println("destroy loginFilter");
58       }
59 
60 }

  controller:

浏览器输入:http://localhost:8083/api/v1/account?username=xdclass

前台显示:

控制台显示:

输入错误的username情况:

浏览器输入:http://localhost:8083/api/v1/account?username=xdclasss

跳转到index.html(可用于提示用户进行登录操作)

 

posted @ 2019-01-09 23:47  清晨的第一抹阳光  阅读(1231)  评论(0编辑  收藏  举报