如何保留我的哈希片段在Angular 2中的openid令牌 [英] How to preserve my hash fragment in Angular 2 for openid token
问题描述
我在Angular 2应用程序中使用oauth进行身份验证。认证后,我得到了哈希后返回url中的所有信息。在我的angular 2程序中进行路由后,哈希部分被完全删除,但我需要它。这个问题之前已经被问及回答了,但是答案并没有解决我的问题,我的片段仍然是空。我想如其中一个答案中提到的那样。此解决方案仅适用于pathlocation策略,但我使用hashlocationstrategy
感谢互联网上某个地方的建议,但无法再找到它给它信贷,我用window.location.hash。
在app.module.ts中,您必须输入类似的代码:
构造函数(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);
}
}) ;
您基本上需要寻找导航来启动并订阅它,并且一旦您的应用程序识别/范围和导航到它,收到的令牌的解码开始。 decodeToken就是我自己的解码方法。
这需要在同一个文件中有一个路由到警卫的路径,即:
{path:'scope ',component:LoginComponent},
希望这有助于您!
I am using oauth for authentication in my Angular 2 application. After authentication I get all info in the return url after a hash. After routing inside my angular 2 program, the hash part is totally deleted, but I need it. This questions has been asked and answered before, but the answer did not solve my problem, my fragment is still "null". I think as mentioned in one of the answers. This solution is only for pathlocationstrategy, but I am using hashlocationstrategy
Retrieve hash fragment from url with Angular2
So, this is why the answer there does not solve my issue. Did anybody solve that issue while using "hashlocationstrategy"?
Thanks to a suggestion somewhere on the internet, but cannot find it anymore to give it credit, I used "window.location.hash". In the app.module.ts you have to put similar code:
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);
}
});
You are essentially to looking for a navigation to start and subscribe to it and as soon as your application is identifying /scope and navigating to it, the decoding of the received token starts. "decodeToken" is just my own method for decoding. This entails having a path for routing in your guard in the same file, namely:
{ path: 'scope', component: LoginComponent},
Hope this helps!
这篇关于如何保留我的哈希片段在Angular 2中的openid令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!