How to determine client ip Address In Java interceptor for API route in WSO2 microgatway 3.1.0?

381 Views Asked by At

Hi am using java interceptor and want to determine client ip address who invoked the api

Open Api Specification

paths:
  /test:
    get:
      description: ""
      operationId: PING
      x-wso2-disable-security: true
      x-wso2-throttling-tier: 6PerMin
      x-wso2-request-interceptor: java:org.mgw.interceptor.IDSAuthInterceptor
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PING"
            application/xml:
              schema:
                $ref: "#/components/schemas/PING"
      security:
        - basicAuthentication: []

Java Interceptor

public class IDSAuthInterceptor implements Interceptor {
    public boolean interceptRequest(Caller caller, Request request) {
       // How to determine client ip. Not able to fetch using {Caller caller, Request request}
    }
}
1

There are 1 best solutions below

2
Pubci On

There are two options available.

  1. Use X-Forwarded-For

From the request, you can look for the header values [1].

  1. Use the caller as follows to get the address [2]

caller.getRemoteAddress()

It seems like there is an issue in the Ballerina side which does not return the IP address.

[1] - https://github.com/wso2/product-microgateway/blob/ba689b71e17cb36f77115eaee3334d305eecad28/components/micro-gateway-interceptor/src/main/java/org/wso2/micro/gateway/interceptor/Request.java#L162

[2] - https://github.com/Rajith90/MGW-demo/blob/master/mgwinterceptors/src/main/java/org/mgw/interceptor/SampleInterceptor.java#L40