How to get client IP address in AEM | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Sanjay_Bangar
Sanjay_BangarAccepted solution
New Participant
Oct 11, 2021 4:51 PM UTC

Hi @kbitra1998 ,

         You need to add something in configuration like below.

APACHE CONFIGURATION FILE: /etc/httpd/conf.d/httpd.conf

<IfModule remoteip_module> RequestHeader edit X-Forwarded-For ([0-9]+(?:\.[0-9]+){3})(:[0-9]+)? $1 early # valid for ALB, ELB, AppGateway or Load Balancer + CloudFront RemoteIPHeader X-Forwarded-For # valid for ALB, ELB, AppGateway or Load Balancer + Akamai RemoteIPHeader True-Client-IP </IfModule>

The above change will make sure the user’s IP address is added to the request header. The name of the header is X-Forwarded-For. 

 

Also, the header will have 2 IP addresses ( comma separated ). We have to make sure we read the first IP address from the CSV string.

 

12 replies

Asutosh_Jena_
New Participant
Oct 12, 2021 2:54 AM UTC

Hi @kbitra1998 

 

You can write one Util class in JAVA and add the following code into it.

 

public static String getClientIPAddress(HttpServletRequest request) {
String xForwardedForHeader = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
if (xForwardedForHeader == null) {
return request.getRemoteAddr();
} else {
return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
}
}

 

 This will always return the client IP address irrespective of ALB, ELB, CDN whatever.

 

Thanks! 

Translate
Level 4
Oct 12, 2021 5:13 AM UTC

@asutosh_jena_  I will write the above code and deploy the jar. where can I see the clientip address in the headers part or anywhere else?

Translate
Asutosh_Jena_
New Participant
Oct 12, 2021 5:16 AM UTC

This methods return type is String. So you can print in log and check if you are getting the value or not. So based on your requirement wherever you need in program, you can use this method.

 

Thanks!

Translate
Sanjay_Bangar
Sanjay_BangarAccepted solution
New Participant
Oct 11, 2021 4:51 PM UTC

Hi @kbitra1998 ,

         You need to add something in configuration like below.

APACHE CONFIGURATION FILE: /etc/httpd/conf.d/httpd.conf

<IfModule remoteip_module> RequestHeader edit X-Forwarded-For ([0-9]+(?:\.[0-9]+){3})(:[0-9]+)? $1 early # valid for ALB, ELB, AppGateway or Load Balancer + CloudFront RemoteIPHeader X-Forwarded-For # valid for ALB, ELB, AppGateway or Load Balancer + Akamai RemoteIPHeader True-Client-IP </IfModule>

The above change will make sure the user’s IP address is added to the request header. The name of the header is X-Forwarded-For. 

 

Also, the header will have 2 IP addresses ( comma separated ). We have to make sure we read the first IP address from the CSV string.

 

Translate
Level 2
Jan 4, 2022 9:50 AM UTC

Hi @sanjay_bangar ,

 

I have added the code snippet in https.conf, but still I'm not seeing the x-forwarded-for in developer tools. will it be visible in developer tools - request headers?

Translate