在 Vaadin 7 网络应用程序中获取用户的 IP 地址和其他客户端信息
Get user’s IP address, and other client-side info in Vaadin 7 web app
在用户 computer/device 的 Vaadin 7, how does one get the IP address 中?
我可以获取有关客户的其他信息吗?
瓦丁 WebBrowser
WebBrowser
class in Vaadin 7 provides an easy way to access information about the client’s computing environment. Access a WebBrowser
object via the current Page
对象。
WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
IP 地址
getAddress
方法提供客户端的表观IP地址computer/device。
String ipAddress = webBrowser.getAddress();
if ( ipAddress == null ) {
// If null, this Vaadin app is probably running inside a portlet.
}
其他客户信息
WebBrowser
class 可以很容易地告诉你很多关于客户端的信息。
示例:如果客户端是 Mac 或触摸设备(pad 或 phone),则哪个浏览器引擎(Safari、Chrome、Firefox 等),如果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/Servlet
您可以通过 HTTP Request information via the standard Java Servlet 调用获取此客户端信息。但是上面介绍的Vaadin的WebBrowser
class比较方便
示例代码
这是我自己的应用程序中的一些实际代码,此处作为示例显示。这可能不是漂亮或理想的代码,但它让您了解如何越过栅栏查看客户端 Web 浏览器的环境。
一些日期时间工作使用 Joda-Time 库作为此代码的唯一依赖项。
通过 Vaadin 提供的便利 classes ( VaadinSession
and WrappedSession
) 获取标准 Servlet 会话标识符。
String sessionId = VaadinSession.getCurrent().getSession().getId();
让我们获取并使用那个 WebBrowser
对象。
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;
创建所有这些信息的字符串表示形式。
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 }
细心的 reader 可能会注意到 IP 地址被报告为 IPv6 rather than the more usual IPv4. Already reported in Ticket # 8614。
对于 Vaadin 7 之前的 Vaadin 应用程序,请参阅 this Forum thread。
在用户 computer/device 的 Vaadin 7, how does one get the IP address 中?
我可以获取有关客户的其他信息吗?
瓦丁 WebBrowser
WebBrowser
class in Vaadin 7 provides an easy way to access information about the client’s computing environment. Access a WebBrowser
object via the current Page
对象。
WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
IP 地址
getAddress
方法提供客户端的表观IP地址computer/device。
String ipAddress = webBrowser.getAddress();
if ( ipAddress == null ) {
// If null, this Vaadin app is probably running inside a portlet.
}
其他客户信息
WebBrowser
class 可以很容易地告诉你很多关于客户端的信息。
示例:如果客户端是 Mac 或触摸设备(pad 或 phone),则哪个浏览器引擎(Safari、Chrome、Firefox 等),如果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/Servlet
您可以通过 HTTP Request information via the standard Java Servlet 调用获取此客户端信息。但是上面介绍的Vaadin的WebBrowser
class比较方便
示例代码
这是我自己的应用程序中的一些实际代码,此处作为示例显示。这可能不是漂亮或理想的代码,但它让您了解如何越过栅栏查看客户端 Web 浏览器的环境。
一些日期时间工作使用 Joda-Time 库作为此代码的唯一依赖项。
通过 Vaadin 提供的便利 classes ( VaadinSession
and WrappedSession
) 获取标准 Servlet 会话标识符。
String sessionId = VaadinSession.getCurrent().getSession().getId();
让我们获取并使用那个 WebBrowser
对象。
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;
创建所有这些信息的字符串表示形式。
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 }
细心的 reader 可能会注意到 IP 地址被报告为 IPv6 rather than the more usual IPv4. Already reported in Ticket # 8614。
对于 Vaadin 7 之前的 Vaadin 应用程序,请参阅 this Forum thread。