SpringBoot中配置过滤器

在SpringBoot中使用@WebFilter注解标注在一个类上,可以配置一个过滤器,并却该类需要继承HttpFilter,本次使用过滤器在控制台中输出HTTP的路径以及请求方式。

@WebFilter
@Log4j2
public class DefaultFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info(request.getRemoteAddr() + " " + request.getMethod() + " " + request.getRequestURI());
        chain.doFilter(request, response);
    }
}
@SpringBootApplication
@ServletComponentScan("top.mxzero.filter")
public class StartMicroBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartMicroBootApplication.class);
    }
}

并且在配置类上使用@ServletComponentScan注解去扫描过滤器所在的包。

 在每次HTTP请求之后查看控制台的输出信息:


版权声明:本文为qq_45425562原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。