user1552545
user1552545

Reputation: 1331

How to get the sender IP Address in spring integration?

I use Spring integration for Web service message handling. Unfortunately the Message does not contains the sender IP Address. How can I get this information?

@Bean
public SimpleWebServiceInboundGateway myInboundGateway() {
    SimpleWebServiceInboundGateway simpleWebServiceInboundGateway = new SimpleWebServiceInboundGateway();
    simpleWebServiceInboundGateway.setRequestChannelName("testChannel");
    simpleWebServiceInboundGateway.setReplyChannelName("testResponseChannel");
    return simpleWebServiceInboundGateway;
}

@ServiceActivator(inputChannel = "testChannel", outputChannel = "testResponseChannel")
public Message getHeaders(Message message) {
    // how can I reach the sender IP address here
    return message;
}

Upvotes: 3

Views: 1077

Answers (2)

Artem Bilan
Artem Bilan

Reputation: 121560

The SimpleWebServiceInboundGateway doesn't map transport headers by default. See DefaultSoapHeaderMapper. Of course you can implement your own, but that really might be enough for you to use:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
              .getRequest()
              .getRemoteAddr();

in that your target @ServiceActivator.

Of course that will work if you don't shift message to a different thread before the service activator. The RequestContextHolder.currentRequestAttributes() is tied with ThreadLocal.

Upvotes: 4

Salman Salman
Salman Salman

Reputation: 227

You can retrieve it from HttpServletRequest, using getRemoteAddr() to get access to user IP address and getHeader() to get header value. This is assuming you can modify your controller class.

Perhaps this will help:

@Controller
public class MyController {
    @RequestMapping(value="/do-something")
    public void doSomething(HttpServletRequest request) {
        final String userIpAddress = request.getRemoteAddr();
        final String userAgent = request.getHeader("user-agent");
        System.out.println (userIpAddress);
    }
}

Upvotes: 0

Related Questions