Skip to content

Commit

Permalink
refactor: 프록시를 사용하지 않는 경우도 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nayonsoso committed Oct 25, 2024
1 parent f2e35a1 commit 69b5461
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions backend/src/main/java/reviewme/global/RequestLimitInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

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

private final RedisTemplate<String, Long> redisTemplate;
private final RequestLimitProperties requestLimitProperties;
Expand All @@ -46,17 +47,15 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
private String generateRequestKey(HttpServletRequest request) {
String requestURI = request.getRequestURI();
String userAgent = request.getHeader(USER_AGENT);
String ip = extractIp(request);
String ip = extractIpAddress(request);

return String.format("RequestURI: %s, IP: %s, UserAgent: %s", requestURI, ip, userAgent);
}

private String extractIp(HttpServletRequest request) {
String xForwardedForIP = request.getHeader(X_FORWARDED_FOR);
String xRealIp = request.getHeader(X_REAL_IP);
if (xForwardedForIP == null) {
return xRealIp;
}
return xForwardedForIP;
private String extractIpAddress(HttpServletRequest request) {
return PROXY_HEADERS.map(request::getHeader)
.filter(Objects::nonNull)
.findFirst()
.orElse(request.getRemoteAddr());
}
}

0 comments on commit 69b5461

Please sign in to comment.