gpt4 book ai didi

javascript - Google OAuth 2.0 "Request for Permission"网页无法关闭(FF、Chrome)

转载 作者:行者123 更新时间:2023-12-03 12:03:56 28 4
gpt4 key购买 nike

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

当我调用电话请求用户授权访问其 Google 日历时,默认浏览器中会按预期显示“请求许可”网页。用户单击“取消”或“接受”后,网页会显示以下消息:

Received verification code. Closing...

在 Internet Explorer 中,网页随后关闭。然而,在 Firefox 和 Chrome 中,网页保持打开状态。

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

这是我用来触发网页的代码(取自 Google Developer's CalendarSample ( 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 来“解决”该问题。我已将网页上显示的消息文本更改为:

Received verification code. Closing...

至:

Your response has been received.

Please close this window.

可以在此处找到 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

关于javascript - Google OAuth 2.0 "Request for Permission"网页无法关闭(FF、Chrome),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271201/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com