JohnAlva commented on code in PR #4825:
URL: https://github.com/apache/fineract/pull/4825#discussion_r2193538775


##########
fineract-core/src/main/java/org/apache/fineract/infrastructure/core/filters/GeolocationHeaderFilter.java:
##########
@@ -0,0 +1,93 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fineract.infrastructure.core.filters;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+@RequiredArgsConstructor
+@Slf4j
+public class GeolocationHeaderFilter extends OncePerRequestFilter {
+
+    private final FineractProperties fineractProperties;
+
+    private static final String[] IP_HEADER_CANDIDATES = { "X-Forwarded-For", 
"Proxy-Client-IP", "WL-Proxy-Client-IP",
+            "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", 
"HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR",
+            "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR" };
+
+    public static String getClientIpAddress(HttpServletRequest request) {
+        for (String header : IP_HEADER_CANDIDATES) {
+            String ip = request.getHeader(header);
+            if (ip != null && ip.length() != 0 && 
!"unknown".equalsIgnoreCase(ip)) {
+                log.debug("SEND IP : {}", ip);
+                return ip;
+            }
+        }
+        log.debug("getRemoteAddr method : {}", request.getRemoteAddr());
+        return request.getRemoteAddr();
+    }
+
+    @Override
+    protected void doFilterInternal(final HttpServletRequest request, final 
HttpServletResponse response, final FilterChain filterChain)
+            throws IOException, ServletException {
+        FineractProperties.FineractGeolocationProperties geolocationProperties 
= fineractProperties.getGeolocation();
+        if (geolocationProperties.isEnabled()) {
+            handleClientIp(request, response, filterChain, 
geolocationProperties);
+        } else {
+            filterChain.doFilter(request, response);
+        }
+
+    }
+
+    private void handleClientIp(HttpServletRequest request, 
HttpServletResponse response, FilterChain filterChain,
+            FineractProperties.FineractGeolocationProperties 
geolocationProperties) throws IOException, ServletException {
+        try {
+            String clientIpAddress = getClientIpAddress(request);
+            if (StringUtils.isNotBlank(clientIpAddress)) {
+                log.info("Found Client IP in header : {}", clientIpAddress);
+                ClientIpHolder.setClientIp(clientIpAddress);

Review Comment:
   In a typical multi-threaded server application (e.g., Spring Boot app), each 
HTTP request is handled by a thread (possibly from a thread pool). If you want 
to track the client IP address throughout the processing of that request (e.g., 
across multiple service layers), storing it in a ThreadLocal variable allows 
you to:
   
   Access the IP anywhere in the call stack without passing it as a parameter.
   Keep data isolated per request/thread.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to