Skip to content

Commit

Permalink
api rate limit filter remove deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cankurttekin committed Nov 15, 2024
1 parent 5a60ccf commit c8f89fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
import java.io.IOException;
import java.time.Duration;

import static java.time.Duration.ofSeconds;

@Component
public class RateLimitingFilter extends OncePerRequestFilter {

private final Bucket bucket = Bucket.builder()
.addLimit(Bandwidth.classic(60, Refill.greedy(60, Duration.ofMinutes(1)))) // 10 requests per minute
.addLimit(limit -> limit.capacity(60).refillGreedy(60, Duration.ofMinutes(1)))
.build();


@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (bucket.tryConsume(1)) {
filterChain.doFilter(request, response);
} else {
response.setStatus(429); // HTTP 429 Too Many Requests
response.setStatus(429); // HTTP 429 TOO_MANY_REQUESTS
response.getWriter().write("Too many requests. Please try again later.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http, RateLimitingFi
).permitAll()
.anyRequest().authenticated()
);
// Add Rate Limitin Filter

// Add Rate Limiting Filter
http.addFilterBefore(rateLimitingFilter, UsernamePasswordAuthenticationFilter.class);
// Add JWT token filter
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

return http.build();
}

Expand Down

0 comments on commit c8f89fa

Please sign in to comment.