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;

}

}

                                                                       微信公众号: 

                                               10.Spring-Boot中如何使用filter(过滤器)_第1张图片

                                                                             JAVA程序猿成长之路

                          分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。 

你可能感兴趣的:(spring-boot,Spring-Boot)