gpt4 book ai didi

angularjs - 服务器从包含#(哈希)符号的 AngularJs 页面重定向到登录 URL

转载 作者:行者123 更新时间:2023-12-03 08:14:38 25 4
gpt4 key购买 nike

我使用 Spring Security 来处理 session 超时。我的应用程序还使用 AngularJs

我有以下问题:

如果 session 超时并且用户从 angularJs 页面向安全 url 发出非 ajax 请求,Spring Security 会将我重定向到登录 url 但保留 #(哈希)之后的 Url 部分签名

让我举例说明:

  • 一个用户位于/myapp/foo#!/bar
  • session 超时
  • 用户问题获得对安全 url 的请求
  • 用户被重定向到/myapp/signin#!/bar

如何避免将 #!/bar 部分添加到登录页面?

修改:上面的描述有一点小错误。有问题的用例如下:

  • 用户未登录并尝试访问 protected 资源,例如:localhost:8080/bignibou/curriculum/#/view/1(例如,通过将此 url 粘贴到浏览器的地址栏中).
  • 它们将自动重定向到以下 Url:http://localhost:8080/bignibou/signin#/view/1

注意井号和之后的部分被保留。请注意,与 Spring Security 相比,这与 AngularJs 的关系更大,因为 Spring Security 不包含哈希符号(哈希从未出现在 chrome 开发工具的网络选项卡中这一事实证实了这一点...)

最佳答案

与 Angular 不同,这听起来像是大多数现代浏览器(我相信是 IE9 之后)实现的 HTTP/3xx 重定向哈希片段的预期行为。这里有关于细微差别的详细解释:

URL Fragments and Redirects

官方文档在这里:

W3C URI handling - Section 4.1 .

编辑:

为了修改(或者,在本例中,删除)片段,您可以利用 HTML5 的历史状态。尝试将此添加到您的登录页面:

history.replaceState("", document.title, window.location.pathname + window.location.search)

有关 replaceState 的更多信息,在 Adding and modifying history entries 中有很好的记录在 Mozilla 开发者文档中。

这里明显的警告是,您需要它在哪个版本的 IE 中工作。通过快速谷歌,您似乎正在查看 IE10 之后的版本,因此对于早期版本,您需要更基本的东西,例如:

window.location.hash = ""

这将删除片段但保留 #,但是嘿,这是 IE。

因此,作为复合解决方案,您可以使用:

if("replaceState" in history)
// proper browsers
history.replaceState("", document.title, window.location.pathname + window.location.search)
else
// fallback for ie
window.location.hash = ""

关于angularjs - 服务器从包含#(哈希)符号的 AngularJs 页面重定向到登录 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21702899/

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