我正在尝试实施Google Oauth功能.这就是我的程序运行方式.首先,index.jsp文件运行并打开一个页面,如下所示:
我的index.jsp代码是:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Website</title> </head> <style> h1 { font-family: Bookman; font-size: 150px; font-style: normal; font-variant: normal; font-weight: 800; line-height: 26.4px; text-color: white; text-align: center; } p { font-family: Bookman; font-size: 50px; font-style: normal; font-variant: normal; font-weight: 800; line-height: 26.4px; text-color: white; text-align: center; } .main_background{ background-image: url("img/main_page_background.jpg"); -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } </style> <body class=main_background> <h1 style="color:white">Title</h1> <p style="color:white">Subtitle</p> <form action="google_oauth" method="post"> <input type="submit" value="Sign In With Google"/> </form> </body> </html>
用户点击“使用Google按钮登录”会重定向到servlet.在servlet内部,使用以下代码打开另一个html文件:
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { File htmlFile = new File("C:\\Users\\neel-\\OneDrive\\Eclipse Workspace\\Website\\Website\\WebContent\\google_oauth.html"); Desktop.getDesktop().browse(htmlFile.toURI()); } }
我知道我可以将其实现到.jsp文件中,但进一步说我将需要重定向.打开html文件后会发生什么,我们会得到一个这样的窗口.
打开的html文件是google_ouath.html文件,代码为:
<!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script> var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?'; var VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='; var SCOPE = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; var CLIENTID = '778647136201-d9trubpsuokohuj9a0c9bgufpo1qvqtf.apps.googleusercontent.com'; var REDIRECT = 'http://localhost:8080/Website/main_page' var logoUT = 'http://accounts.google.com/logout'; var TYPE = 'token'; var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE; var acToken; var tokenType; var expiresIn; var user; var loggedIn = false; function login() { var win = window.open(_url,"windowname1",'width=800,height=600'); var pollTimer = window.setInterval(function() { try { console.log(win.document.URL); if (win.document.URL.indexOf(REDIRECT) != -1) { window.clearInterval(pollTimer); var url = win.document.URL; acToken = gup(url,'access_token'); tokenType = gup(url,'token_type'); expiresIn = gup(url,'expires_in'); win.close(); validateToken(acToken); } } catch(e) { } },500); } function validateToken(token) { $.ajax({ url: VALIDURL + token,data: null,success: function(responseText){ getUserInfo(); loggedIn = true; $('#loginText').hide(); $('#logoutText').show(); },dataType: "jsonp" }); } function getUserInfo() { $.ajax({ url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,success: function(resp) { user = resp; console.log(user); $('#uName').text('Welcome ' + user.name); $('#imgHolder').attr('src',user.picture); },dataType: "jsonp" }); } function gup(url,name) { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\#&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( url ); if( results == null ) return ""; else return results[1]; } function startlogoutPolling() { $('#loginText').show(); $('#logoutText').hide(); loggedIn = false; $('#uName').text('Welcome '); $('#imgHolder').attr('src','none.jpg'); } </script> </head> <body> <a href='#' onClick='login();' id="loginText"> Click here to login </a> <a href="#" style="display:none" id="logoutText" target='myIFrame' onclick="myIFrame.location='https://www.google.com/accounts/logout'; startlogoutPolling();return false;"> Click here to logout</a> <iframe name='myIFrame' id="myIFrame" style='display:none'></iframe> <div id='uName'></div> <img src='' id='imgHolder'/> </body> </html>
但是,此问题的相关代码段是:
function login() { var win = window.open(_url,500); }
在上面,我们可以看到谷歌登录已完成并且网址已重定向.但是回到login()函数,win.close()实际上并没有关闭弹出窗口.我在网上搜索,发现一些无效的解决方案.例如,here它声明使用变通方法
open(location,'_self').close();
但这也不起作用.
还有一件事是在Brock Adams Solution它说:
with one small exception,javascript must not be allowed to close a window that was not opened by that same javascript.
但是,我不认为这是一个问题.
问题:当win.close()不起作用时,如何关闭此弹出窗口.
更新:尝试b4tch的anser之后.我得到以下内容: