首页 > 解决方案 > 如何在 WSO2 microgatway 3.1.0 中的 API 路由的 Java 拦截器中确定客户端 IP 地址?

问题描述

嗨,我正在使用 java 拦截器,想确定调用 api 的客户端 IP 地址

开放 API 规范

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 拦截器

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}
    }
}

标签: wso2wso2-amwso2-mgw

解决方案


有两种选择。

  1. 使用 X-Forwarded-For

从请求中,您可以查找标头值 [1]。

  1. 如下使用调用者获取地址[2]

调用者.getRemoteAddress()

Ballerina 方面似乎存在不返回 IP 地址的问题。

[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


推荐阅读