Skip to content
Closed
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
Expand Up @@ -29,6 +29,7 @@
*
* @author Ben Alex
* @author Luke Taylor
* @author Lazar Radinović
*/
public class WebAuthenticationDetails implements Serializable {

Expand All @@ -44,7 +45,7 @@ public class WebAuthenticationDetails implements Serializable {
* @param request that the authentication request was received from
*/
public WebAuthenticationDetails(HttpServletRequest request) {
this(request.getRemoteAddr(), extractSessionId(request));
this(getClientIp(request), extractSessionId(request));
}

/**
Expand All @@ -58,6 +59,20 @@ public WebAuthenticationDetails(String remoteAddress, String sessionId) {
this.sessionId = sessionId;
}

private static String getClientIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip != null && !ip.isBlank()) {
// Take the first IP (original client)
return ip.split(",")[0].trim();
}

// Alternative proxy header
ip = request.getHeader("X-Real-IP");

// Fallback to direct client ip
return (ip != null && !ip.isBlank()) ? ip : request.getRemoteAddr();
}

private static String extractSessionId(HttpServletRequest request) {
HttpSession session = request.getSession(false);
return (session != null) ? session.getId() : null;
Expand Down