Getting IP Address Using CXF

Getting IP Address Using CXF explains about How to get IP Address of client who is consuming the CXF service

I am showing here, How to capture the remote IP Address of clients, both SOAP & RESTFul services

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);

On this code, we are getting HttpServletRequest and from that taking the remote ip address

You can see the below example, which is finding client ip address using cxf


This tutorial is also applicable for JAX-WS Web services because PhaseInterceptorChain.getCurrentMessage() applicable for both JAX-WS and JAX-RS based services

You can change CXF Web Service Tutorial according to the below tutorial, if you are using JAX-WS based service

Getting IP Address Using CXF

I am going to re-use CXF Restful Tutorial

I just added a new property "studentIP" into student object. please find the code below

package com.student;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Student")
public class Student {
private String name;
private String studentIP;

public String getName() {
return name;

public void setName(String name) { = name;

public String getStudentIP() {
return studentIP;

public void setStudentIP(String studentIP) {
this.studentIP = studentIP;


We are making changes ChangeStudentDetailsImpl class, just adding

PhaseInterceptorChain.getCurrentMessage() and getting HttpServletRequest, so that we can access remote ip of clients consuming our service

package com.student;

import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
public Student changeName(Student student) {
student.setName("HELLO " + student.getName());
// Here We Capture/Extracting Client IP Address Using CXF
Message message = PhaseInterceptorChain.getCurrentMessage();
    HttpServletRequest request =
return student;

public Student getName() {
Student student = new Student();
return student;

1 2 Next

6 Responses to "Getting IP Address Using CXF"
  1. Becker 2012-03-20 07:43:31.0
  1. admin 2012-03-21 07:43:31.0
  1. guru 2012-03-22 07:43:31.0
  1. admin 2012-03-23 07:43:31.0
  1. Joseph 2012-03-24 07:43:31.0
  1. admin 2012-03-25 07:43:31.0

Your email address will not be published. Required fields are marked *