Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] refactor: TooManyRequest 로깅에 사용되는 IP 추출 로직 변경 #944

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package reviewme.config.requestlimit;

import static org.springframework.http.HttpHeaders.USER_AGENT;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
Expand All @@ -17,6 +17,8 @@
@RequiredArgsConstructor
public class RequestLimitInterceptor implements HandlerInterceptor {

private static final List<String> PROXY_HEADERS = List.of("X-FORWARDED-FOR", "X-REAL-IP");

private final RedisTemplate<String, Long> redisTemplate;
private final RequestLimitProperties requestLimitProperties;

Expand All @@ -40,9 +42,12 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons

private String generateRequestKey(HttpServletRequest request) {
String requestURI = request.getRequestURI();
String remoteAddr = request.getRemoteAddr();
String userAgent = request.getHeader(USER_AGENT);
String remoteAddress = PROXY_HEADERS.stream()
.map(request::getHeader)
.filter(Objects::nonNull)
.findFirst()
.orElse(request.getRemoteAddr());

return String.format("RequestURI: %s, RemoteAddr: %s, UserAgent: %s", requestURI, remoteAddr, userAgent);
return String.format("RequestURI: %s, RemoteAddress: %s", requestURI, remoteAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RequestLimitInterceptorTest {
private final ValueOperations<String, Long> valueOperations = mock(ValueOperations.class);
private final RequestLimitProperties requestLimitProperties = mock(RequestLimitProperties.class);
private final RequestLimitInterceptor interceptor = new RequestLimitInterceptor(redisTemplate, requestLimitProperties);
private final String requestKey = "RequestURI: /api/v2/reviews, RemoteAddr: localhost, UserAgent: Postman";
private final String requestKey = "RequestURI: /api/v2/reviews, RemoteAddress: localhost";

@BeforeEach
void setUp() {
Expand Down