0%

SpringBoot Restful API拦截方式

SpringBoot RestFul API拦截大概有三种方式:Filter、Interceptor、Aspect。

Filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;
import java.util.Date;

@Component
public class TimerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Time filter init");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Time filter start");
long startTime = new Date().getTime();
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("time filter:"+(new Date().getTime()-startTime));
System.out.println("time filter finish");
}

@Override
public void destroy() {
System.out.println("Time filter destroy");
}
}

Interceptor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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.persistence.Convert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
* this is spring interceptor
*
* @author hsj
* @create 2017-11-11 18:16
**/

@Component
public class TimeInterceptor implements HandlerInterceptor {

/**
* 控制器方法处理之前
*
* @param httpServletRequest
* @param httpServletResponse
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
System.out.println("preHandle");
System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
System.out.println(((HandlerMethod) handler).getMethod().getName());
httpServletRequest.setAttribute("startTime", new Date().getTime());
return false;
}

/**
* 控制器方法处理之后
* 控制器方法调用不抛异常调用
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
Long startTime = (Long) httpServletRequest.getAttribute("startTime");
System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime));
}

/**
* 控制器方法抛不抛异常都会被调用
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion");
Long startTime = (Long) httpServletRequest.getAttribute("startTime");
System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime));
System.out.println("ex is" + e);
}
}

Aspect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
* this is a acpect
* 切入点
* 在那些方法上起作用
* 在什么时候起作用
*
* @author hsj
* @create 2017-11-11 20:52
**/
@Aspect
@Component
public class TimeAspect {
@Around("execution(* com.nbkj.controller.UserController.*(..))")
public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("time aspect start");
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg.getClass().getName());
System.out.println("arg is " + arg);
}
long startTime = new Date().getTime();
Object obj = proceedingJoinPoint.proceed();
System.out.println("time aspect 耗时" + (new Date().getTime() - startTime));
System.out.println("time aspect end");
return obj;
}
}

总结

  • 过滤器(Filter) :可以拿到原始的http请求,但是拿不到你请求的控制器和请求控制器中的方法的信息。

  • 拦截器(Interceptor):可以拿到你请求的控制器和方法,却拿不到请求方法的参数。

  • 切片 (Aspect) : 可以拿到方法的参数,但是却拿不到http请求和响应的对象