Search code examples
spring-integrationspring-integration-dslspring-integration-http

Best way to get Remote IP in Http Inbound Spring Integration


How can I get remote IP in spring integration (Http Inbound ). I have searched through stack overflow , I could not relate answers to what I want to achieve.

    @Bean
    public IntegrationFlow inquiry(IntegrationFlow outboundGateway){
        return IntegrationFlows.from(Http.inboundGateway("/inquiry")
                .errorChannel("errorChannel")
                .requestPayloadType(String.class)
                .requestMapping(m -> m.methods(HttpMethod.POST)))
                .wireTap(Loggers.REQUEST_LOGGER_CHANNEL)
                .handle((p,h)->{
                    log.info("REMOTE IP: {}",...)
                    return p;
                })
                .transform(Transformers.fromJson(PayloadDTO.class))
                ...;
    }

Solution

  • log.info("REMOTE IP: {}", 
             ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getRemoteAddr()) 
    

    Also see this: https://mkyong.com/java/how-to-get-client-ip-address-in-java/