-
Notifications
You must be signed in to change notification settings - Fork 24
/
PathMatchInterceptor.java
96 lines (82 loc) · 3.33 KB
/
PathMatchInterceptor.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.woowacourse.pickgit.authentication.presentation.interceptor;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class PathMatchInterceptor implements HandlerInterceptor {
private final HandlerInterceptor handlerInterceptor;
private final HashMap<String, List<HttpMethod>> includeRegistry;
private final HashMap<String, List<HttpMethod>> excludeRegistry;
private final PathMatcher pathMatcher;
public PathMatchInterceptor(HandlerInterceptor handlerInterceptor) {
this.handlerInterceptor = handlerInterceptor;
this.pathMatcher = new AntPathMatcher();
this.includeRegistry = new HashMap<>();
this.excludeRegistry = new HashMap<>();
}
public PathMatchInterceptor addPathPatterns(String pattern, HttpMethod... methods) {
return addPathPatterns(pattern, Arrays.asList(methods));
}
public PathMatchInterceptor addPathPatterns(String pattern, List<HttpMethod> methods) {
this.includeRegistry.putIfAbsent(pattern, methods);
return this;
}
public PathMatchInterceptor excludePatterns(String pattern, List<HttpMethod> methods) {
this.excludeRegistry.putIfAbsent(pattern, methods);
return this;
}
public PathMatchInterceptor excludePatterns(String pattern, HttpMethod... methods) {
return excludePatterns(pattern, Arrays.asList(methods));
}
@Override
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler
) throws Exception {
String requestUrl = request.getRequestURI();
String requestMethod = request.getMethod();
for (String url : excludeRegistry.keySet()) {
boolean isMethodMatch = excludeRegistry.get(url)
.stream()
.map(Enum::name)
.anyMatch(method -> method.equals(requestMethod));
if (pathMatcher.match(url, requestUrl) && isMethodMatch) {
return true;
}
}
for (String url : includeRegistry.keySet()) {
boolean isMethodMatch = includeRegistry.get(url)
.stream()
.map(Enum::name)
.anyMatch(method -> method.equals(requestMethod));
if (pathMatcher.match(url, requestUrl) && isMethodMatch) {
return handlerInterceptor.preHandle(request, response, handler);
}
}
return true;
}
@Override
public void postHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView
) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(
HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex
) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}