[英]Azure functions in Java - Get remote address IP
我正在尋找一種在 Java 中的 Azure 函數中獲取遠程 IP 地址的解決方案。 來自 HttpRequestMessage 的標頭僅包含主機。 我找到了 .NET 的解決方案,但像往常一樣,Java 更難。
您可以從請求頭x-forwarded-for 中獲取 IP:
@FunctionName("test")
public HttpResponseMessage getConfig(
@HttpTrigger(
name = "request",
methods = {HttpMethod.GET},
route = "test",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
ExecutionContext context
) {
// Prints all headers
for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
context.getLogger().info(header.getKey() + " - " + header.getValue());
}
// Prints the header containing the IP address in the format ip:port
context.getLogger().info(request.getHeaders().get("x-forwarded-for"));
...
}
請注意,IP 地址附帶了一個端口。 例如:123.123.213.213:88999
所以你只需要對字符串進行處理以刪除端口部分。
注意: x-forwarded-for 標頭僅存在於真實的 Azure Functions 環境中。 如果您在本地環境中測試它,您將找不到這樣的標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.