package com.lzh.web.filter;
import javax.servlet.*;
import java.io.IOException;
import java.util.Date;
/**
* @author li
* 2019/10/1 16:09
* version 1.0
*/
// 可以通过注解注入 或者通过MVC配置加载
//@Component
public class TimeFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器 filter init....");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("过滤器 进入..doFilter..");
long start = System.currentTimeMillis();
chain.doFilter(request, response);
System.out.println("过滤器 执行用时: " + (System.currentTimeMillis() - start));
System.out.println("过滤器 拦截器结束...");
}
@Override
public void destroy() {
System.out.println("过滤器 filter destroy....");
}
}
通过配置加载 Filter
package com.lzh.web.config;
import com.lzh.web.filter.TimeFilter;
import com.lzh.web.interceptor.TimeInterceptior;
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.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* @author li
* 2019/10/1 16:45
* version 1.0
*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
private TimeInterceptior timeInterceptior;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(timeInterceptior);
}
@Bean
public FilterRegistrationBean timeFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new TimeFilter());
List url = new ArrayList<>();
url.add("/*");
filterRegistrationBean.setUrlPatterns(url);
return filterRegistrationBean;
}
}
package com.lzh.web.interceptor;
import org.omg.PortableInterceptor.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;
import java.util.Date;
/**
* @author li
* 2019/10/1 19:57
* version 1.0
*/
@Component
public class TimeInterceptior implements HandlerInterceptor {
//前置
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器 preHandle...");
System.out.println("拦截器 类名字: "+((HandlerMethod) handler).getBean().getClass().getName());
System.out.println("拦截器 方法名字: "+((HandlerMethod)handler).getMethod().getName());
request.setAttribute("start", System.currentTimeMillis());
return true;
}
//没有遇到异常 就进入
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("拦截器 postHandle...");
Long start = (Long) request.getAttribute("start");
System.out.println("拦截器 postHandle 耗时: " + (System.currentTimeMillis() - start));
}
//结束
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("拦截器 afterCompletion...");
Long start = (Long) request.getAttribute("start");
System.out.println("拦截器 afterCompletion 耗时: " + (System.currentTimeMillis() - start));
System.out.println("拦截器 异常类型: " + ex);
}
}
package com.lzh.web.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* @author li
* 2019/10/2 11:23
* version 1.0
*/
@Aspect
@Component
public class TimeAspect {
@Around("execution(* com.lzh.web.controller.UserController.*(..))")
public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("AOP 开始...");
Long start = System.currentTimeMillis();
Object[] args = pjp.getArgs();
for (Object arg : args) {
System.out.println("参数: " + arg);
}
Object object = pjp.proceed();
System.out.println("AOP结束 end: "+(System.currentTimeMillis() - start));
return object ;
}
}
过滤器 进入..doFilter..
拦截器 preHandle...
拦截器 类名字: com.lzh.web.controller.UserController$$EnhancerBySpringCGLIB$$963e03f
拦截器 方法名字: getinfo
AOP 开始...
参数: 1
进入..getinfo...
AOP结束 end: 5
拦截器 postHandle...
拦截器 postHandle 耗时: 95
拦截器 afterCompletion...
拦截器 afterCompletion 耗时: 95
拦截器 异常类型: null
过滤器 执行用时: 115
过滤器 拦截器结束...
过滤器可以拿到原始http的请求和相应信息,无法获得处理请求的方法的信息
拦截器可以获得过滤器获得的内容,但是无法获得调用http请求 具体的参数信息
切面 只可以获得调用的 具体参数
注意:本文归作者所有,未经作者允许,不得转载