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
  • 8263 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 14, 2021

Our requiremnt is we need to get the ClientIp from dispatcher/ anywhere from AEM and pass it to the API which is already in place, that API will give the response.

 


@kbitra1998  API will be invoked at the server side, so you can read the value directly in the backend code something like below and it should solve the issue.

 

Let's say you want to pass IP in the below method in API:

 

APIObject.setIpAddress(Util.getClientIPAddress(request));

 

Thanks!