[java] client ip 구하기
package com.web.cms.core.common.util;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
public class NetworkUtil
{
public static String getClientIp( HttpServletRequest request ) throws UnknownHostException
{
String ip = request.getHeader( "X-Forwarded-For" );
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "Proxy-Client-IP" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "WL-Proxy-Client-IP" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "HTTP_CLIENT_IP" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "HTTP_X_FORWARDED_FOR" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "X-Real-IP" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "X-RealIP" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getHeader( "REMOTE_ADDR" );
}
if( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase( ip ) )
{
ip = request.getRemoteAddr();
}
//로컬 다이렉트 접속
if( ip.equals( "0:0:0:0:0:0:0:1" ) || ip.equals( "127.0.0.1" ) )
{
InetAddress address = InetAddress.getLocalHost();
ip = address.getHostName() + "/" + address.getHostAddress();
}
return ip;
}
}
static 으로 만들어서 request 만 있으면 아무데서나 사용