zoukankan      html  css  js  c++  java
  • Filter in Springboot

    首先用如下代码说明 TestFilter(过滤路径是 /*)和 TestWebFilter ( 过滤路径是 /test) 的实现方法

    • TestFilter 只实现 Filter 接口, 并用 @Component 标注。
    • TestWebFilter 实现 Filter接口, 并用@WebFilter 标注, 且在启动类加 @ServletComponentScan 注解。
    • 此处的Filter 方法会在过滤到对应的请求时打印出请求的 RequestURI

     代码:

    https://github.com/XLuffyStory/SpringBootStudy/tree/master/filter_demo

    当 GET localhost:8081/hello 的时候,TestFilter(过滤路径是 /*) 和 TestWebFitler(过滤路径是 /test), 只有 TestFilter 会过滤到 /hello ,所以会有一条log.

     

    当GET localhost:8081/test 的时候,因为有TestFilter(过滤路径是 /*) 和 TestWebFitler(过滤路径是 /test),所以会有两条log.

     下面说为什么用Filter:

    1. filter 的一种用法:可以在filter中根据条件决定是否调用chain.doFilter(request, response)方法, 即是否让目标资源(Controller 方法)执行。

     当在TestWebFilter 中注释掉 chain.doFilter(request, response); GET localhost:8081/test 是没有返回值的,即如下Controller 方法没有得到执行

    @GetMapping("/test")
        public String test() {
        return "Hello Test";
        }

     2. 另一种用法是在 doFilter 方法中做一些预处理:

     Reference:

    https://www.cnblogs.com/ooo0/p/10360952.html

    https://www.cnblogs.com/huanzi-qch/p/11239167.html

  • 相关阅读:
    react-echarts之折线图的显示
    Log4j2
    测试驱动开发Junit4
    JavaWeb基础: Cookie
    JavaWeb前端:Bootstrap基础
    JavaWeb前端:CSS
    JavaWeb前端:JQuery
    Android基础:Activity
    JavaWeb前端: JavaScript 简介
    JavaWeb前端:HTML5 简介
  • 原文地址:https://www.cnblogs.com/luffystory/p/14038847.html
Copyright © 2011-2022 走看看