【问题标题】:JWT token not cleared on the browser even after token expiry or logging out即使在令牌过期或注销后,浏览器上也不会清除 JWT 令牌
【发布时间】:2019-01-07 06:31:01
【问题描述】:

我们有一个带有 Angular 2 的 spring-boot 应用程序,我们使用 JWT 进行身份验证。我遇到了一个问题 - 即使令牌已过期然后注销并再次登录,过期的 JWT 令牌仍然在请求标头上可用。所以后端仍然验证旧的 JWT 令牌,而不是新的。我需要清除浏览器缓存以使其正常工作。

我看到令牌存储在 SessionStorage 中,下面是清除logout() 上的令牌

logout() {
    if (this.principal.isAuthenticated()) {
        sessionStorage.removeItem('authenticationtoken');
        this.authServerProvider.logout().subscribe();            
    }
    this.principal.authenticate(null);
}

但这也不会清除旧令牌。我该怎么做才能从浏览器中清除令牌?

【问题讨论】:

  • 确保您的令牌存储在 sessionStorage 而不是 localStorage 中,如果这样,它将是 localStorage.removeItem('authenticationtoken')
  • @willmaz 令牌存储在 sessionStorage 中。
  • sessionStorage.removeItem('authenticationtoken') 应该从网络存储中删除您的令牌...您可以使用 F12 -> application -> session storage 在浏览器中直接尝试,然后执行上面的代码在控制台中验证令牌是否已经存在
  • 当我点击注销按钮时,我可以看到令牌已从 F12 -> 应用程序 -> 会话存储中删除,但我仍然在后端收到旧令牌。
  • 可以在帖子中添加登录服务吗?

标签: angular authentication jwt


【解决方案1】:

在挖掘代码后,我发现标头被定义为“const”。当我将它们更改为“let”时,一切都开始正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 2018-03-30
    • 2019-07-04
    • 2020-11-14
    • 2020-08-05
    • 2019-08-03
    • 2021-07-23
    • 2017-08-14
    相关资源
    最近更新 更多