Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to get client IP address in AEM

kbitra1998
Level 3
Level 3

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

 

 

1 Accepted Solution
bangar50
Correct answer by
Level 7
Level 7

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.

 

View solution in original post

5 Replies
bangar50
Correct answer by
Level 7
Level 7

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.

 

View solution in original post

vijayselvas1
Level 2
Level 2

Hi @bangar50 ,

 

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

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! 

kbitra1998
Level 3
Level 3

@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

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!

kbitra1998
Level 3
Level 3

@Asutosh_Jena_  how gonna we call the above function from frontend, I mean do we need pass request parameter in the above function,if yes can you help me out how we gonna pass the paramters request.

 

I have attached the screenshots below both function and how are we calling this function from frontend for your reference..

 

kbitra1998_0-1634188940835.png

 

kbitra1998_1-1634189625993.png

 

Thank you so much for your help on this.

Asutosh_Jena_
Community Advisor
Community Advisor

Hi @kbitra1998 

 

Can you tell me your requirement here?

Why you want to read the client IP address on frontend? It should be used on the backend.

 

Also retrieving the IP address on frontend using SlingModel/HTL will not work as the SlingModel will be cached at the server side.

 

Thanks!

kbitra1998
Level 3
Level 3

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.

 

Asutosh_Jena_
Community Advisor
Community Advisor

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

vijayselvas1
Level 2
Level 2

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>

 

@bangar50 

 

Regards,

Vijay

Asutosh_Jena_
Community Advisor
Community Advisor

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!

vijayselvas1
Level 2
Level 2

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