-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete network before building + Prevent increase too many requests
- Loading branch information
1 parent
63b8b15
commit 5db7f07
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
marketplace-service/src/main/java/com/axonivy/market/config/RateLimitFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.axonivy.market.config; | ||
|
||
import io.github.bucket4j.Bandwidth; | ||
import io.github.bucket4j.Bucket; | ||
import io.github.bucket4j.Refill; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Component | ||
public class RateLimitFilter extends OncePerRequestFilter { | ||
private static final String REQUEST_PATH = "/api"; | ||
@Value("${market.allowed.download-capacity}") | ||
private int capacity; | ||
private final Map<String, Bucket> buckets = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
String clientIp = getClientIp(request); | ||
String apiPath = request.getRequestURI(); | ||
|
||
if (apiPath.contains(REQUEST_PATH)) { | ||
Bucket bucket = buckets.computeIfAbsent(clientIp, this::createNewBucket); | ||
|
||
if (bucket.tryConsume(1)) { | ||
System.out.println("Request allowed for IP: " + clientIp + ". Remaining tokens: " + bucket.getAvailableTokens()); | ||
filterChain.doFilter(request, response); | ||
} else { | ||
System.out.println("Too many requests from IP: " + clientIp); | ||
response.setStatus(HttpServletResponse.SC_BAD_GATEWAY); | ||
response.getWriter().write("Too many requests. Please try again later."); | ||
} | ||
} else { | ||
filterChain.doFilter(request, response); | ||
} | ||
} | ||
|
||
private Bucket createNewBucket(String clientIp) { | ||
Bandwidth limit = Bandwidth.classic(capacity, Refill.greedy(10, Duration.ofMinutes(2))); | ||
return Bucket.builder().addLimit(limit).build(); | ||
} | ||
|
||
private String getClientIp(HttpServletRequest request) { | ||
String forwardedFor = request.getHeader("X-Forwarded-For"); | ||
if (forwardedFor != null && !forwardedFor.isEmpty()) { | ||
return forwardedFor.split(",")[0]; | ||
} | ||
return request.getRemoteAddr(); | ||
} | ||
} |