10.Spring-Boot中如何使用filter(过滤器)

2021-12-03 08:03:11 浏览数 (1)

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

代码语言:javascript复制
1.定义LogFilter.javapackage com.niugang.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import ...                            

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说10.Spring-Boot中如何使用filter(过滤器),希望能够帮助大家进步!!!

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

1.定义LogFilter.java

代码语言:javascript复制
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

代码语言:javascript复制
此代码由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;

}

}

0 人点赞