com.databasesandlife.util.servlet.IpAddressDeterminer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util.servlet;
import org.slf4j.LoggerFactory;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpAddressDeterminer {
public @CheckForNull InetAddress getRequestIpAddress(@Nonnull HttpServletRequest request) {
try {
var ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null) ipAddress = request.getRemoteAddr();
return ipAddress == null ? null : InetAddress.getByName(ipAddress);
}
catch (UnknownHostException e) {
LoggerFactory.getLogger(getClass()).warn("Unexpected exception when fetching IP address, will ignore: " + e.getMessage());
return null;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy