Get user’s IP address, and other client-side info in Vaadin 7 web app

馋奶兔 提交于 2020-02-23 09:42:28

问题


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

Can I get other information about the client?


回答1:


Vaadin WebBrowser

The 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 object.

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

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

Other Client Info

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

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 / Servlet

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.

Example Code

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.

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

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

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

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( " }" );

Example output:

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

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


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



来源:https://stackoverflow.com/questions/28823945/get-user-s-ip-address-and-other-client-side-info-in-vaadin-7-web-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!