How to get the client ip address in a easy way through AEM either using dispatcher or any other method.
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
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?
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!
@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?
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!
@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..
Thank you so much for your help on this.
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!
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!
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>
Regards,
Vijay
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