我正在尝试实施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>
&#13;
用户点击&#34;使用Google按钮登录&#34;会发生什么?重定向到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());
}
}
&#13;
我知道我可以将其实现到.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,
data: null,
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>
&#13;
然而,此问题的相关代码是:
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);
}
&#13;
上面,我们可以看到谷歌登录已完成并且网址已重定向。但是回到login()函数,win.close()实际上并没有关闭弹出窗口。我在网上搜索,发现一些无效的解决方案。例如,here它声明使用解决方法
open(location, '_self').close();
但这也不起作用。
还有一点是在Brock Adams Solution中声明:
有一个小例外,不得允许javascript关闭一个未被同一个javascript打开的窗口。
但是,我不认为这是一个问题。
问题:当win.close()不起作用时,如何关闭此弹出窗口。
答案 0 :(得分:3)
不得允许Javascript关闭未被同一个javascript打开的窗口。这是出于安全原因而做的。
可能只是必须调用自身位置对象,配置打开的窗口必须由同一个java脚本打开。为此,如果您尝试使用open(location, '_self').close();
而不是win.close()
,那么它应该适用于我。如果它仍然不起作用请给我。