Facebook回调已开始将#_=_散列下划线附加到返回URL
有人知道为什么吗?解决方案是什么?
发布于 2011-09-04 15:14:17
会话重定向行为中的
更改
本周,当此字段保留为空时,我们开始向redirect_uri中添加片段#____=____。请确保您的应用程序可以处理此行为。
为了防止这种情况,在你的登录url请求中设置redirect_uri如下:(使用Facebook php-sdk)
$facebook->getLoginUrl(array('redirect_uri' => $_SERVER['SCRIPT_URI'],'scope' => 'user_about_me'));更新
以上就是documentation所说的解决这个问题的方法。然而,Facebook记录的解决方案并不起作用。请考虑在Facebook Platform Updates blog post上发表评论,并关注this bug以获得更好的答案。在此之前,请在head标签中添加以下内容以解决此问题:
<script type="text/javascript">
if (window.location.hash && window.location.hash == '#_=_') {
window.location.hash = '';
}
</script>或者一个更详细的替代方案(感谢niftylettuce):
<script type="text/javascript">
if (window.location.hash && window.location.hash == '#_=_') {
if (window.history && history.pushState) {
window.history.pushState("", document.title, window.location.pathname);
} else {
// Prevent scrolling by storing the page's current scroll offset
var scroll = {
top: document.body.scrollTop,
left: document.body.scrollLeft
};
window.location.hash = '';
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scroll.top;
document.body.scrollLeft = scroll.left;
}
}
</script>发布于 2013-08-19 08:47:07
TL;DR
if (window.location.hash === "#_=_"){
history.replaceState
? history.replaceState(null, null, window.location.href.split("#")[0])
: window.location.hash = "";
}包含分步说明的完整版
// Test for the ugliness.
if (window.location.hash === "#_=_"){
// Check if the browser supports history.replaceState.
if (history.replaceState) {
// Keep the exact URL up to the hash.
var cleanHref = window.location.href.split("#")[0];
// Replace the URL in the address bar without messing with the back button.
history.replaceState(null, null, cleanHref);
} else {
// Well, you're on an old browser, we can get rid of the _=_ but not the #.
window.location.hash = "";
}
}一步一步:
当浏览器支持
fragment为#_=_.#上拆分并只取第一部分来清理URL。history用干净的URL替换当前页面状态。这将修改当前的历史记录条目,而不是创建新的历史条目。这意味着“后退”和“前进”按钮将以您想要的方式工作。;-)
#_-_。了解有关history.replaceState的更多信息。
了解有关window.location的更多信息。
发布于 2017-01-29 11:25:35
这是Facebook出于安全原因而设计实现的。以下是Facebook团队成员埃里克·奥斯古德的解释:
这已被标记为“有意设计”,因为它可以防止潜在的安全漏洞。
一些浏览器会将来自URL的散列片段附加到它们被重定向到的新URL的末尾(如果该新URL本身没有散列片段)。
例如,如果example1.com返回到example2.com的重定向,则转到example1.com#abc的浏览器将转到example2.com#abc,example1.com中的散列片段内容将可由example2.com上的脚本访问。
由于可以将一个身份验证流重定向到另一个身份验证流,因此可以让来自一个应用程序的敏感身份验证数据可供另一个应用程序访问。
通过将新的散列片段附加到重定向URL以防止此浏览器行为,可以缓解此问题。
如果生成的URL的外观或客户端行为令人担忧,则可以使用window.location.hash (甚至是您自己的服务器端重定向)来删除违规字符。
https://stackoverflow.com/questions/7131909
复制相似问题