zoukankan      html  css  js  c++  java
  • 10.Spring-Boot中如何使用filter(过滤器)

    在springboot中如何使用filter(过滤器),其实这个现实可以用aop来实现。

    1.定义LogFilter.java

    package com.niugang.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    
    import javax.servlet.FilterChain;
    
    import javax.servlet.FilterConfig;
    
    import javax.servlet.ServletException;
    
    import javax.servlet.ServletRequest;
    
    import javax.servlet.ServletResponse;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    
    
    import org.slf4j.Logger;
    
    import org.slf4j.LoggerFactory;
    
    
    
    /**
    
    * 记录日志的filter
    
    * @author niugang
    
    *
    
    */
    
    public class LogFilter implements Filter {
    
    private static Logger logger = LoggerFactory.getLogger(LogFilter.class);
    
    @Override
    
    public void init(FilterConfig filterConfig) throws ServletException {
    
    logger.info("初始化filter");
    
    
    
    }
    
    
    
    @Override
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    
    throws IOException, ServletException {
    
    HttpServletRequest req =(HttpServletRequest)request;
    
    HttpServletResponse resp =(HttpServletResponse)response;
    
    logger.info("执行filter................");
    
    chain.doFilter(req, resp);
    
    
    
    }
    
    
    
    @Override
    
    public void destroy() {
    
    logger.info("filter被销毁");
    
    }
    
    
    
    }
    
    package com.niugang.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    
    import javax.servlet.FilterChain;
    
    import javax.servlet.FilterConfig;
    
    import javax.servlet.ServletException;
    
    import javax.servlet.ServletRequest;
    
    import javax.servlet.ServletResponse;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    
    
    import org.slf4j.Logger;
    
    import org.slf4j.LoggerFactory;
    
    
    
    /**
    
    * 记录日志的filter
    
    * @author niugang
    
    *
    
    */
    
    public class LogFilter implements Filter {
    
    private static Logger logger = LoggerFactory.getLogger(LogFilter.class);
    
    @Override
    
    public void init(FilterConfig filterConfig) throws ServletException {
    
    logger.info("初始化filter");
    
    
    
    }
    
    
    
    @Override
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    
    throws IOException, ServletException {
    
    HttpServletRequest req =(HttpServletRequest)request;
    
    HttpServletResponse resp =(HttpServletResponse)response;
    
    logger.info("执行filter................");
    
    chain.doFilter(req, resp);
    
    
    
    }
    
    
    
    @Override
    
    public void destroy() {
    
    logger.info("filter被销毁");
    
    }
    
    
    
    }

    2.配置filter

    FilterConfig.java

    package com.niugang;
    
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    
    import org.springframework.context.annotation.Bean;
    
    import org.springframework.context.annotation.Configuration;
    
    import com.niugang.filter.LogFilter;
    
    
    
    /**
    
    * 过滤器配置文件
    
    * @author niugang
    
    *springboot启动类虎自动扫描过滤器配置文件
    
    */
    
    @Configuration
    
    public class FilterConfig {
    
    /**
    
    * z自定义过滤器
    
    * @return
    
    */
    
    @Bean
    
    public LogFilter logFilter() {
    
    return new LogFilter();
    
    }
    
    /**
    
    * 注册过滤器
    
    * @return
    
    */
    
    @Bean
    
    public FilterRegistrationBean testFilterRegistration() {
    
    FilterRegistrationBean registration = new FilterRegistrationBean();
    
    registration.setFilter(logFilter());
    
    registration.addUrlPatterns("/*");
    
    registration.setOrder(1);
    
    return registration;
    
    }
    
    }

    微信公众号

                              
  • 相关阅读:
    常用设计模式:装饰者模式
    常用数据结构算法 : 堆排序
    常用数据结构算法:二叉树的最近公共祖先
    java网络通信:HTTP协议 之 Sessions与Cookies
    java网络通信:HTTP协议
    常见的设计模式:工厂模式
    Java基础:类加载机制
    一个C++右值引用的问题
    剖析一个用C++写的行情交易系统
    C++ Coroutine简明教程
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12196823.html
Copyright © 2011-2022 走看看