检测Javafx webview [英] Detect Javafx webview
问题描述
我的问题是如何检测网页是否使用java webview访问,如果没有断开连接或重定向客户端以帮助降低垃圾邮件认证请求。
JavaFX WebView允许您设置用户代理字符串, 用户代理属性在WebEngine上。您可以使用它来将用户代理设置为您希望的任何内容。然后,当WebView客户端请求网页时,它将在用户代理标头中提供此值。您的服务器代码可以查询用户代理标题,然后根据结果采取它认为必要的任何操作。
来自这个答案,用户代理字符串的默认输出为:
用户代理:Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit / 537.44(KHTML,如Gecko)JavaFX / 8.0 Safari / 537.44
$ c $如WebEngine文档中所述,此值可能会在JavaFX版本和操作系统之间发生变化。很可能,缺省值总是包含文本 JavaFX
,所以如果用户代理字符串包含文本 JavaFX code>。否则,如果你想超级确定,那么你可以编写你的JavaFX客户端来明确地设置用户代理字符串为你在服务器上检查的值。
这表明你会写这样的内容:
< c:if test =$ {fn:contains(header ['User-Agent'],'JavaFX')}>< / c:如果>
So I am creating a java desktop program. This programs goes to a authentication webpage running on jsp. And does so using the javafx webview(which runs on WebKit)
My question is how can I detect the if the webpage is being accessed using java webview then if not disconnect or redirect the client in order to help lower spam authentication request.
解决方案 The JavaFX WebView allows you to set a user agent string, using the user agent property on the WebEngine. You can use this to set the user agent to anything that you wish. Then when the WebView client requests a web page, it will supply this value in the user agent header. Your server code can query the user agent header and then take whatever action it deems necessary based upon the result.
From this answer, the default output for the user agent string was:
User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.44 (KHTML, like Gecko) JavaFX/8.0 Safari/537.44
As noted in the WebEngine documentation, this value can change between JavaFX versions and operating systems. Most likely, the default will always include the text JavaFX
, so you could just check on the server if the user agent string contains the text JavaFX
. Otherwise, if you want to be super-sure, then you can code your JavaFX client to explicitly set the user agent string to a value which you check on your server.
To see how to check a user agent in a JSP, refer to:
Which would indicate that you would write something like this:
<c:if test="${fn:contains(header['User-Agent'],'JavaFX')}"></c:if>
这篇关于检测Javafx webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!