如何在 Angular 2 中为 openid 令牌保留我的哈希片段

How to preserve my hash fragment in Angular 2 for openid token

我在 Angular 2 应用程序中使用 oauth 进行身份验证。身份验证后,我在哈希后得到 return url 中的所有信息。在我的 angular 2 程序内部路由后,哈希部分被完全删除,但我需要它。这个问题之前有人问过也有人回答过,但是回答没有解决我的问题,我的片段还是"null"。我认为正如其中一个答案中提到的那样。此解决方案仅适用于 pathlocationstrategy,但我使用的是 hashlocationstrategy

所以,这就是为什么那里的答案没有解决我的问题。有人在使用 "hashlocationstrategy" 时解决了这个问题吗?

多亏了互联网上某处的建议,但找不到它来证明它,我使用了 "window.location.hash"。 在 app.module.ts 你必须输入类似的代码:

constructor(public routerName: Router) {
console.log("Appmodule is starting")
routerName.events.subscribe(s => {
  if (s instanceof NavigationStart&&
           (s.toString().indexOf("/scope")>0) )
  { let tokenReceived=window.location.hash;
    this.decodeToken(tokenReceived);
  }
});

您实际上是在寻找一个导航来启动和订阅它,一旦您的应用程序识别 /scope 并导航到它,接收到的令牌的解码就会开始。 "decodeToken" 只是我自己的解码方法。 这需要在同一个文件中有一个路由路径,即:

{ path: 'scope', component: LoginComponent},

希望对您有所帮助!