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 2
January 4, 2022

Hi Asutosh,

 

I have added the below snippet in conf file. but still, I'm not seeing the X-Forwarded-For in the browser developer console. is it something x-Forwarded-For will not visible in the browser?

 

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

 

@sanjay_bangar 

 

Regards,

Vijay

Level 2
January 4, 2022

Hi @vijayselvas1 

 

This code will be executed at server side and you will never be able to read the IP address on browser console. If you want to read the IP address on clientside, you will need to make an AJAX call and get the IP address and populate it on the frontend.

 

But why you want to read the IP address at clientside? What exact functionality you are expecting here?

 

Thanks!


Hi @asutosh_jena_ ,

 

I wanted to read the user IP address in server side and for specific IP I just wanted to have some logic. Since I have added the x-forwarded-for in dispatcher, I thought it will be visible in request-header section in developer tools. Let me try to get the IP in server side.  Thanks