構(gòu)建Zuul自定義過(guò)濾器,限制ip頻繁請(qǐng)求
自定義zuul過(guò)濾器其實(shí)很簡(jiǎn)單
1. 首先pom文件得先引入zuul依賴
1
2
3
4
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-netflix-zuul</ artifactId > </ dependency > |
2. 創(chuàng)建一個(gè)類,繼承自ZuulFilter
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
|
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.exception.ZuulException; import org.springframework.stereotype.Component; /** * 構(gòu)建zuul自定義過(guò)濾器 */ @Component public class MyFilter extends ZuulFilter { /** * 定義過(guò)濾器的類型 * pre:在請(qǐng)求被路由之前執(zhí)行 * route:在路由請(qǐng)求的時(shí)候執(zhí)行 * post:請(qǐng)求路由以后執(zhí)行 * error:處理請(qǐng)求時(shí)發(fā)生錯(cuò)誤的時(shí)候執(zhí)行 * * @return 過(guò)濾器的類型 */ @Override public String filterType() { return "pre" ; } /** * 過(guò)濾器執(zhí)行的順序,配置多個(gè)有順序的過(guò)濾 * 執(zhí)行順序從小到大 * * @return 執(zhí)行順序 */ @Override public int filterOrder() { return 1 ; } /** * 是否開啟過(guò)濾器 * true:開啟 * false:禁用 * * @return 是否開啟過(guò)濾器 */ @Override public boolean shouldFilter() { return true ; } /** * 過(guò)濾器的業(yè)務(wù)實(shí)現(xiàn) * * @return null 沒(méi)有意義 * @throws ZuulException 異常信息 */ @Override public Object run() throws ZuulException { System.out.println( "per zuul filter..." ); return null ; } } |
自定義類上需要加上 @Component 注解
a. filterType()方法,定義過(guò)濾器的類型,返回的就是字符串,有以下4種類型
- pre:在請(qǐng)求被路由之前執(zhí)行
- route:在路由請(qǐng)求的時(shí)候執(zhí)行
- post:請(qǐng)求路由以后執(zhí)行
- error:處理請(qǐng)求時(shí)發(fā)生錯(cuò)誤的時(shí)候執(zhí)行
b. filterOrder()方法,過(guò)濾器執(zhí)行的順序
c. shouldFilter()方法,是否開啟過(guò)濾器,true開啟,false不開啟
d. run()方法,過(guò)濾器的業(yè)務(wù)實(shí)現(xiàn),在這里寫實(shí)現(xiàn)邏輯的具體代碼
3. 限制ip頻繁請(qǐng)求,示例代碼
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
97
98
99
100
101
102
103
104
105
106
|
import com.imooc.grace.result.GraceJsonResult; import com.imooc.grace.result.ResponseStatusEnum; import com.imooc.utils.IPUtil; import com.imooc.utils.JsonUtils; import com.imooc.utils.RedisOperator; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /** * 限制ip頻繁請(qǐng)求 */ @Component public class BlackIpFilter extends ZuulFilter { /** * ip連續(xù)請(qǐng)求的次數(shù) */ private static final int CONTINUE_COUNTS = 10 ; /** * ip判斷的時(shí)間間隔,單位秒 */ private static final int TIME_INTERVAL = 10 ; /** * 限制的時(shí)間,單位秒 */ private static final int LIMIT_TIMES = 15 ; @Autowired private RedisOperator redisOperator; @Override public String filterType() { return "pre" ; } @Override public int filterOrder() { // 這里設(shè)置為2,上面那個(gè)過(guò)濾器設(shè)置為1,則執(zhí)行順序?yàn)?1->2,大家可以測(cè)試一下 return 2 ; } @Override public boolean shouldFilter() { return true ; } @Override public Object run() throws ZuulException { // 獲取上下文對(duì)象 RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); // 獲取ip String requestIp = IPUtil.getRequestIp(request); // 判斷該ip在10秒內(nèi)請(qǐng)求次數(shù)是否超過(guò)10次,超過(guò)則限制該ip15秒內(nèi)不能訪問(wèn),15秒后再放行 final String ipRedisKey = "zuul-ip:" + requestIp; final String ipRedisLimitKey = "zuul-ip-limit:" + requestIp; // 獲取當(dāng)前ip這個(gè)key的剩余時(shí)間 long limitLeftTime = redisOperator.ttl(ipRedisLimitKey); // 判斷該ip是否還有剩余時(shí)間 if (limitLeftTime > 0 ) { stopRequest(currentContext); return null ; } // 在redis中累加ip的請(qǐng)求次數(shù) long requestCounts = redisOperator.increment(ipRedisKey, 1 ); if (requestCounts == 1 ) { redisOperator.expire(ipRedisKey, TIME_INTERVAL); } if (requestCounts > CONTINUE_COUNTS) { // 限制ip訪問(wèn) redisOperator.set(ipRedisLimitKey, ipRedisLimitKey, LIMIT_TIMES); stopRequest(currentContext); } return null ; } private void stopRequest(RequestContext context) { // 停止zuul繼續(xù)向下路由,禁止請(qǐng)求通信 context.setSendZuulResponse( false ); // 返回響應(yīng)碼200 context.setResponseStatusCode( 200 ); // TODO 要返回提示的json內(nèi)容(可以自定義任何響應(yīng)內(nèi)容) // 例如 {"status":544,"msg":"請(qǐng)求過(guò)于頻繁,請(qǐng)稍后再試","success":false,"data":null} String result = "json內(nèi)容" ; // 設(shè)置返回內(nèi)容 context.setResponseBody(result); // 設(shè)置編碼 context.getResponse().setCharacterEncoding( "utf-8" ); // 設(shè)置返回內(nèi)容格式為json context.getResponse().setContentType(MediaType.APPLICATION_JSON_VALUE); } } |
這里使用了redis來(lái)記錄ip請(qǐng)求次數(shù)和控制時(shí)間間隔
獲取ip工具類 IPUtil
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
|
import javax.servlet.http.HttpServletRequest; /** * 獲取ip工具類 */ public class IPUtil { /** * 獲取請(qǐng)求IP: * 用戶的真實(shí)IP不能使用request.getRemoteAddr() * 這是因?yàn)榭赡軙?huì)使用一些代理軟件,這樣ip獲取就不準(zhǔn)確了 * 此外我們?nèi)绻褂昧硕嗉?jí)(LVS/Nginx)反向代理的話,ip需要從X-Forwarded-For中獲得第一個(gè)非unknown的IP才是用戶的有效ip。 */ public static String getRequestIp(HttpServletRequest request) { String ip = request.getHeader( "x-forwarded-for" ); if (ip == null || ip.length() == 0 || "unknown" .equalsIgnoreCase(ip)) { ip = request.getHeader( "Proxy-Client-IP" ); } if (ip == null || ip.length() == 0 || "unknown" .equalsIgnoreCase(ip)) { ip = request.getHeader( "WL-Proxy-Client-IP" ); } if (ip == null || ip.length() == 0 || "unknown" .equalsIgnoreCase(ip)) { ip = request.getHeader( "HTTP_CLIENT_IP" ); } if (ip == null || ip.length() == 0 || "unknown" .equalsIgnoreCase(ip)) { ip = request.getHeader( "HTTP_X_FORWARDED_FOR" ); } if (ip == null || ip.length() == 0 || "unknown" .equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } } |
到此這篇關(guān)于Spring Cloud Zuul自定義過(guò)濾器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Cloud Zuul過(guò)濾器內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://juejin.cn/post/6940124043441864712