java 如何确定访问了我的网站的 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12107739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:37:01  来源:igfitidea点击:

How to determine by what IP Address my website has been accessed?

javajspurljakarta-eeservlets

提问by Prateek

I have a web application and for that I want to capture the IP address of the clients who are accessing my website so that I can know what region is the most accessing the application. I am using Java EEin my application.

我有一个 Web 应用程序,为此我想捕获访问我网站的客户端的 IP 地址,以便我可以知道访问该应用程序最多的区域。我Java EE在我的应用程序中使用。

Probably there is some solution if we talk about header and when requestis sent from the end-user.

如果我们谈论标头以及何时request从最终用户发送,可能有一些解决方案。

回答by Kapelchik

Use method getRemoteAddr()from interface ServletRequestor methods getHeaders()form interface HttpServletRequest:

使用来自接口ServletRequest 的方法getRemoteAddr()或来自接口HttpServletRequest 的方法getHeaders()

HttpServletRequest httpRequest = (HttpServletRequest) request;
String userIpAddress = httpRequest.getHeader("X-Forwarded-For");

There's one caution for using the method getRemoteAddr:

使用方法getRemoteAddr有一个注意事项:

Sure you can use the method and in general case you will get IP of client. However, the method is useless if an user is behind a proxy. In this case you'll get the proxy server's IP address and not the client. But the proxy may include the requesting client IP in a special HTTPheader. So to retrieve real-client-IP call method getHeader("X-Forwarded-For").

当然你可以使用这个方法,一般情况下你会得到客户端的IP。但是,如果用户位于代理后面,则该方法是无用的。在这种情况下,您将获得代理服务器的 IP 地址,而不是客户端。但是代理可能会在特殊的HTTP标头中包含请求的客户端 IP 。所以要检索真实客户端IP调用方法getHeader("X-Forwarded-For")

An example usage in JSP:

JSP 中的一个示例用法:

Use set value of IP address in session using JSTL:

使用JSTL在会话中使用 IP 地址的设置值:

<c:set var="userIp" value="${requestScope.header('x-forwarded-for')}" scope="session" />

And then get this value from the session in a convenient place.

然后在方便的地方从会话中获取此值。

In JSP you can use <c:out value="${sessionScope.userIp}" />or in servlet as session.getAttribute('userIp');

在 JSP 中,您可以使用<c:out value="${sessionScope.userIp}" />或 在 servlet 中作为session.getAttribute('userIp');

Please read docs:

请阅读文档

java.lang.String getRemoteAddr()returns the Internet Protocol (IP) address of the client or last proxythat sent the request.

java.lang.String getRemoteAddr()返回发送请求的客户端或最后一个代理的 Internet 协议 (IP) 地址。

回答by pratikabu

Along with the above said answers (by Andrey and Stephen) do use some analyticstool also. It will give you a bigger picture of the traffic coming to your website. One such example is Google Analytics.

除了上面所说的答案(安德烈和斯蒂芬),也使用一些analytics工具。它会让您更全面地了解进入您网站的流量。一个这样的例子是谷歌分析

回答by Stephen C

The request object is passed as a Java argument to the servlet method generated from your JSP. The argument's name is ... wait for it ... request.

请求对象作为 Java 参数传递给从 JSP 生成的 servlet 方法。该参数的名字是......等待... request

So you should be able to refer to it in a scriptlet embedded in your JSP; e.g.

因此,您应该能够在嵌入在 JSP 中的 scriptlet 中引用它;例如

    <%
         InetAddress addr = request.getRemoteAddr();
         // blah blah blah
    %>

It is debatable whether you should be doing this kind of thing in a JSP. It is generally accepted as "best practice" that JSPs should onlybe used for formatting output. The business logic of a webapp should be in a controller servlet that then finally forwards to the JSP servlet to generate the HTML (or whatever).

你是否应该在 JSP 中做这种事情是有争议的。JSP 应该用于格式化输出,这被普遍认为是“最佳实践” 。webapp 的业务逻辑应该在控制器 servlet 中,然后最终转发到 JSP servlet 以生成 HTML(或其他)。

If you are going to put business logic into a JSP, scriptlets tend to be difficult to maintain ... though if you are justgoing to display the IP address, that shouldn't be a concern. (The preferred approach from an engineering perspective is to use tags and JSP expression language.)

如果您打算将业务逻辑放入 JSP 中,那么 scriptlet 往往难以维护……不过如果您只是打算显示 IP 地址,则不必担心。(从工程角度来看,首选方法是使用标记和 JSP 表达式语言。)

Finally, there is no guarantee that the IP address returned by getRemoteAddr()will be the actual client IP address. It could be the address of a proxy, or even a complete fabrication.

最后,不能保证 返回的 IP 地址getRemoteAddr()将是实际的客户端 IP 地址。它可能是代理的地址,甚至是完全捏造的。