How to get client IP address in AEM | Community
Skip to main content
Level 4
October 11, 2021
Solved

How to get client IP address in AEM

  • October 11, 2021
  • 2 replies
  • 8224 views

How to get the client ip address in a easy way through AEM either using dispatcher or any other method.

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sanjay_Bangar

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.

 

2 replies

Sanjay_Bangar
Community Advisor
Sanjay_BangarCommunity AdvisorAccepted solution
Community Advisor
October 11, 2021

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.

 

Level 2
January 4, 2022

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?

Asutosh_Jena_
Community Advisor
Community Advisor
October 12, 2021

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! 

Level 4
October 12, 2021

@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?

Asutosh_Jena_
Community Advisor
Community Advisor
October 12, 2021

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!