⒈使用Filter(过滤器)
package cn.coreqi.security.filter; import org.springframework.stereotype.Component; import javax.servlet.*;
import java.io.IOException;
import java.util.Date; @Component
public class TimeFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("TimeFilterc初始化完成!");
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
long start = new Date().getTime();
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("执行方法所用时间:" + (new Date().getTime() - start));
} @Override
public void destroy() {
System.out.println("TimeFilterc已释放!");
}
}
当然,如果是你自己写的Filter一般都会这样,别人的Filter就没有办法使用@Component注解了,那么你可以使用Web.xml,当然SpringBoot没有这个,可以使用SpringBoot独有的方式。
package cn.coreqi.security.config; import cn.coreqi.security.filter.TimeFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.ArrayList;
import java.util.List; @Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean timeFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new TimeFilter());
List<String> urls = new ArrayList<>();
urls.add("/*");
filterRegistrationBean.setUrlPatterns(urls);
return filterRegistrationBean;
}
}
遗憾的是,Filter是Servlet提供的,SpringMVC的一些信息没有办法全部拿到,只能拿到request和response,没有办法拿到是那个控制器,那个方法处理的。这种情况下可以考虑Spring提供的Interceptor(拦截器)。
⒉使用Interceptor(拦截器)
package cn.coreqi.security.interceptor; import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Component
public class TimeInterceptor implements HandlerInterceptor {
/**
* 在控制器方法调用之前执行
* @param request
* @param response
* @param handler 当前控制器方法
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
System.out.println(((HandlerMethod) handler).getMethod().getName());
return true;
} /**
* 在控制器方法调用之后执行,如果控制器方法抛出了异常,那么该方法将不会被调用
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } /**
* 无论控制器方法是否抛出异常,该方法始终会被调用
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
package cn.coreqi.security.config; import cn.coreqi.security.filter.TimeFilter;
import cn.coreqi.security.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.ArrayList;
import java.util.List; @Configuration
public class WebConfig implements WebMvcConfigurer { @Autowired
private TimeInterceptor timeInterceptor; @Bean
public FilterRegistrationBean timeFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new TimeFilter());
List<String> urls = new ArrayList<>();
urls.add("/*");
filterRegistrationBean.setUrlPatterns(urls);
return filterRegistrationBean;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(timeInterceptor);
}
}
拦截器可以拿到请求、响应、处理请求控制器方法的对象,但没法拿到处理请求方法参数的值。如果想要更进一步的拿到具体参数的值,那么就需要用到切片。
⒊使用Aspect(切片)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
package cn.coreqi.security.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; @Aspect //声明当前类为切片类
@Component //让切片成为Spring容器中的一部分
public class TimeAspect { /**
* 第一个*代表任何返回值
* cn.coreqi.security.controller.UserController.* 代表拦截该类(控制器)中的所有方法
* (..)代表该方法可以有任意参数
* @param pjp 表示当前所拦截的信息
* @return
*/
@Around("execution(* cn.coreqi.security.controller.UserController.*(..))")
public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
for (Object arg:pjp.getArgs()){ //遍历拦截方法的参数列表
System.out.println("arg is :" + arg);
}
Object object = pjp.proceed(); //调用真正的方法
return object;
}
}