Google OAuth 2.0 "请求权限"网页不关闭(FF, Chrome)

Google OAuth 2.0 "Request for Permission" webpage does not close (FF, Chrome)

本文关键字:quot FF Chrome 权限 OAuth 请求 Google 请求权 网页      更新时间:2023-09-26

我使用Google OAuth 2.0对已安装的桌面Java应用程序进行身份验证。我正在使用Google OAuth 2.0 Java客户端库。

当我打电话请求用户授权访问他们的Google Calendar时,默认浏览器中显示了"请求权限"网页,正如预期的那样。用户点击取消或接受后,页面显示消息:

收到的验证码。关闭…

在Internet Explorer中,该网页随后被关闭。而在Firefox和Chrome中,网页仍然是打开的。

是否有办法强制网页关闭(从我的Java程序)?

这是我使用的代码,触发网页(取自谷歌开发者的日历样本(http://samples.google-api-java-client.googlecode.com/hg-history/425c5ffc30178f21aea592bc989849ea7e3498fe/calendar-cmdline-sample/instructions.html):

)
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, JSON_FACTORY, clientSecrets,
    Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory)
    .build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

我明白了。未关闭的网页是LocalServerReceiver创建的。LocalServerReciever.CallbackHandler.writeLandingHtml()包含以下内容:

doc.println("<script type='text/javascript'>");
// We open "" in the same window to trigger JS ownership of it, which lets
// us then close it via JS, at least on Chrome.
doc.println("window.setTimeout(function() {");
doc.println("    window.open('', '_self', ''); window.close(); }, 1000);");
doc.println("if (window.opener) { window.opener.checkToken(); }");
doc.println("</script>");

此代码试图利用允许JavaScript关闭页面的漏洞,即使该页面未被JS打开。该漏洞仍然适用于IE 11,但不再适用于FF 31或Chrome 36。

经过大量的搜索,我了解到JS不再可能关闭它没有打开的页面(除了IE中现有的利用漏洞)。

出于我的目的,我通过稍微修改LocalServerReceiver来"解决"问题。我已将网页上显示的消息文本从

更改为:

收到的验证码。关闭…

:

您的回复已收到。

请关闭此窗口。

LocalServerReceiver的(未修改的)源代码可以在这里找到:https://code.google.com/p/google-oauth-java-client/source/browse/google-oauth-client-jetty/src/main/java/com/google/api/client/extensions/jetty/auth/oauth2/LocalServerReceiver.java?r=2624919183758196142f47c414e71db685e77de2