@@ -16,26 +16,17 @@ private UserUtils() {
1616
1717 /**
1818 * Get the client's IP address by checking various headers commonly used by proxies, load balancers, and CDNs.
19- *
20- * Checks headers in order of preference:
21- * 1. X-Forwarded-For (standard proxy header)
22- * 2. X-Real-IP (nginx and other reverse proxies)
23- * 3. CF-Connecting-IP (Cloudflare)
24- * 4. True-Client-IP (Akamai, Cloudflare Enterprise)
25- * 5. Falls back to request.getRemoteAddr()
19+ *
20+ * Checks headers in order of preference: 1. X-Forwarded-For (standard proxy header) 2. X-Real-IP (nginx and other reverse proxies) 3.
21+ * CF-Connecting-IP (Cloudflare) 4. True-Client-IP (Akamai, Cloudflare Enterprise) 5. Falls back to request.getRemoteAddr()
2622 *
2723 * @param request The HttpServletRequest object.
2824 * @return The client's IP address as a String.
2925 */
3026 public static String getClientIP (HttpServletRequest request ) {
3127 // Array of header names to check in order of preference
32- String [] ipHeaders = {
33- "X-Forwarded-For" ,
34- "X-Real-IP" ,
35- "CF-Connecting-IP" ,
36- "True-Client-IP"
37- };
38-
28+ String [] ipHeaders = {"X-Forwarded-For" , "X-Real-IP" , "CF-Connecting-IP" , "True-Client-IP" };
29+
3930 for (String header : ipHeaders ) {
4031 String ip = request .getHeader (header );
4132 if (ip != null && !ip .isEmpty () && !"unknown" .equalsIgnoreCase (ip )) {
@@ -46,17 +37,16 @@ public static String getClientIP(HttpServletRequest request) {
4637 return ip .trim ();
4738 }
4839 }
49-
40+
5041 // Fall back to remote address if no proxy headers found
5142 return request .getRemoteAddr ();
5243 }
5344
5445 /**
5546 * Get the application URL based on the provided request, handling proxy headers properly.
56- *
57- * Checks for forwarded headers (X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port)
58- * to construct the correct URL when behind a proxy or load balancer.
59- * Falls back to standard request properties if no proxy headers are present.
47+ *
48+ * Checks for forwarded headers (X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port) to construct the correct URL when behind a proxy or load
49+ * balancer. Falls back to standard request properties if no proxy headers are present.
6050 *
6151 * @param request The HttpServletRequest object.
6252 * @return The application URL as a String.
@@ -67,7 +57,7 @@ public static String getAppUrl(HttpServletRequest request) {
6757 if (scheme == null || scheme .isEmpty ()) {
6858 scheme = request .getScheme ();
6959 }
70-
60+
7161 // Check for forwarded host
7262 String host = request .getHeader ("X-Forwarded-Host" );
7363 if (host == null || host .isEmpty ()) {
@@ -79,25 +69,22 @@ public static String getAppUrl(HttpServletRequest request) {
7969 host = host .substring (0 , colonIndex );
8070 }
8171 }
82-
72+
8373 // Check for forwarded port
8474 String portHeader = request .getHeader ("X-Forwarded-Port" );
8575 int port ;
86- try {
87- port = Integer .parseInt (portHeader );
88- } catch (NumberFormatException e ) {
89- port = request .getServerPort ();
90- }
91- } else {
76+ try {
77+ port = Integer .parseInt (portHeader );
78+ } catch (NumberFormatException e ) {
9279 port = request .getServerPort ();
9380 }
94-
81+
9582 // Build URL - always include port for backward compatibility
9683 StringBuilder url = new StringBuilder ();
9784 url .append (scheme ).append ("://" ).append (host );
9885 url .append (":" ).append (port );
9986 url .append (request .getContextPath ());
100-
87+
10188 return url .toString ();
10289 }
10390}
0 commit comments