【发布时间】: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