在 Vaadin 7 Web 应用程序中获取用户的 IP 地址和其他客户端信息 [英] Get user’s IP address, and other client-side info in Vaadin 7 web app

查看:23
本文介绍了在 Vaadin 7 Web 应用程序中获取用户的 IP 地址和其他客户端信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vaadin 7 中,如何获得 IP 地址 用户的计算机/设备?

In Vaadin 7, how does one get the IP address of the user’s computer/device?

我可以获得有关客户的其他信息吗?

Can I get other information about the client?

推荐答案

Vaadin WebBrowser

Vaadin 7 中的 WebBrowser 类提供了一种简单的方法访问有关客户端计算环境的信息.通过当前的PageWebBrowser对象一个> 对象.

WebBrowser webBrowser = Page.getCurrent().getWebBrowser();

IP 地址

getAddress 方法提供了明显的客户端计算机/设备的 IP 地址.

IP Address

The getAddress method provides the apparent IP address of the client computer/device.

String ipAddress = webBrowser.getAddress();
if ( ipAddress == null ) {
    // If null, this Vaadin app is probably running inside a portlet.
}

其他客户信息

WebBrowser 类可以轻松地告诉您有关客户端的许多信息.

Other Client Info

The WebBrowser class can easily tell you much information about the client.

示例:如果客户端是 Mac 或触摸设备(平板电脑或手机),使用哪种浏览器引擎(Safari、Chrome、Firefox 等),如果 TLS 已启用 (HTTPS)、屏幕大小、时区和夏令时、区域设置等.甚至还有一种方法可以告诉您 网络浏览器是否太旧而无法与 Vaadin 配合使用.

Examples: if the client is a Mac or a touch device (pad or phone), which browser engine (Safari, Chrome, Firefox, and such), if TLS is engaged (HTTPS), screen size, time zone & Daylight Saving Time, locale, and more. There is even a method to tell you if the web browser is too old to work well with Vaadin.

您可以通过标准 HTTP 请求信息获取此客户端信息="http://en.wikipedia.org/wiki/Java_servlet">Java Servlet 调用.但是上面介绍的 Vaadin 的 WebBrowser 类更方便.

You could get this client-side info by going through the HTTP Request information via the standard Java Servlet calls. But Vaadin’s WebBrowser class described above is more convenient.

这是我自己的应用程序中的一些实际代码,此处作为示例显示.这可能不是漂亮或理想的代码,但它可以让您了解如何越过栅栏查看客户端 Web 浏览器的环境.

Here is some actual code from my own app, shown here as an example. This may not be beautiful or ideal code, but it gives you an idea about how to look over the fence to the client web browser’s environment.

某些日期时间工作使用 Joda-Time 库作为此代码的唯一依赖项.

Some of the date-time work uses the Joda-Time library, as the only dependency for this code.

通过一些方便的类( VaadinSessionWrappedSession )由 Vaadin 提供.

Get the standard Servlet session identifier via some convenience classes ( VaadinSession and WrappedSession ) provided by Vaadin.

String sessionId = VaadinSession.getCurrent().getSession().getId();  

让我们获取并使用该 WebBrowser 对象.>

Let's fetch and use that WebBrowser object.

WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
// Environment stuff
String ipAddress = webBrowser.getAddress(); // May be null, especially if running in a Portlet.
String userAgentInfo = webBrowser.getBrowserApplication();
String touchDevice = String.valueOf( webBrowser.isTouchDevice() );
String screenSize = webBrowser.getScreenWidth() + "x" + webBrowser.getScreenHeight();
String locale = webBrowser.getLocale().toString();
String isHttps = String.valueOf( webBrowser.isSecureConnection() );
// Date-time stuff
DateTime serverNow = DateTime.now( DateTimeZone.UTC );
java.util.Date browserCurrentDate = webBrowser.getCurrentDate();
DateTime browserCurrentDateTime = new DateTime( browserCurrentDate , DateTimeZone.UTC );
String serverClientDifference = new Period( serverNow , browserCurrentDateTime ).toString();
int offset = webBrowser.getTimezoneOffset();
int rawOffset = webBrowser.getRawTimezoneOffset();
Boolean isInDst = webBrowser.isDSTInEffect();
int dst = webBrowser.getDSTSavings();
String timeDescription = "ClientNow→" + browserCurrentDateTime + "/ServerNow→" + serverNow + "/ServerClientDiff→" + serverClientDifference + "/OffsetFromUTC→" + offset + "/RawOffsetFromUTC→" + rawOffset + "/InDST→" + isInDst + "/DST→" + dst;

创建所有这些信息的字符串表示.

Create a string representation of all this info.

StringBuilder description = new StringBuilder();
description.append( "{ Account=" ).append( accountArg );  // Particular to my own app (login).
description.append( " | Username=" ).append( usernameArg );   // Particular to my own app (login).
description.append( " | SessionId=" ).append( sessionId );
description.append( " | IP_Address=" ).append( ipAddress );
description.append( " | HTTPS=" ).append( isHttps );
description.append( " | Locale=" ).append( locale );
description.append( " | TouchDevice=" ).append( touchDevice );
description.append( " | ScreenSize=" ).append( screenSize );
description.append( " | UserAgent=" ).append( userAgentInfo );
description.append( " | Time= " ).append( timeDescription );
description.append( " }" );

示例输出:

{ Account= | Username= | SessionId=9309B2FA176D57F4D74CDC9E4E0238A8 | IP_Address=0:0:0:0:0:0:0:1 | HTTPS=false | Locale=en_US | TouchDevice=false | ScreenSize=1920x1080 | UserAgent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/6.2.3 Safari/537.85.12 | Time= ClientNow→2015-03-03T21:11:25.664Z/ServerNow→2015-03-03T21:11:25.680Z/ServerClientDiff→PT-0.016S/OffsetFromUTC→-28800000/RawOffsetFromUTC→-28800000/InDST→false/DST→3600000 }

细心的读者可能会注意到 IP 地址被报告为 IPv6 而不是更常见的 IPv4.已在 Ticket # 8614 中报告.

The observant reader may notice the IP Address was reported as IPv6 rather than the more usual IPv4. Already reported in Ticket # 8614.

对于 Vaadin 7 之前的 Vaadin 应用,请参阅此论坛主题.

For Vaadin apps before Vaadin 7, see this Forum thread.

这篇关于在 Vaadin 7 Web 应用程序中获取用户的 IP 地址和其他客户端信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆