简体   繁体   English

在重定向到新的 SPA 页面时在标头中传递身份验证令牌

[英]Passing authentication token in header while redirecting to a new SPA page

Assume these:假设这些:

       A -----------------------> B
(Sender website)          (Angular website)

We implemented a normal Angular SPA (B) that its index.html and other resources are simply hosted in IIS and there is a simple rewrite rule for handling the routes in Angular.我们实现了一个普通的 Angular SPA (B) ,它的 index.html 和其他资源简单地托管在 IIS 中,并且有一个简单的重写规则来处理 Angular 中的路由。 Users in Angular need to login and they get a JWT token and it is storing in browser storage. Angular 中的用户需要登录,他们会获得一个 JWT 令牌,并将其存储在浏览器存储中。

There is a website (A) that wants to redirect users to Angular website but we want to pass the JWT token from A to B too, because the tokens are the same and we want to prevent user from logging in again.有一个网站(A)想要将用户重定向到 Angular 网站,但我们也想将 JWT 令牌从 A 传递到 B,因为令牌是相同的,我们希望阻止用户再次登录。

Website A can send the token in a post request header while is redirecting to B. The problem is that JS (Angular) can't directly get the header parameters because they are sending to IIS.网站 A 可以在重定向到 B 时在post 请求标头中发送令牌。问题是 JS (Angular) 无法直接获取标头参数,因为它们正在发送到 IIS。

The question:问题:

  1. Is there a way in IIS, we could get the token from the request and set it in html attribute while retrieving the index.html?在 IIS 中有没有办法,我们可以从请求中获取令牌并在检索 index.html 时将其设置为 html 属性? so then, JS can check it's html elements and will find the token.因此,JS 可以检查它的 html 元素并找到令牌。

  2. Is the above technique correct?上述技术正确吗? if not, could you please give your suggestion?如果没有,你能给出你的建议吗?

My thought process on this would be to redirect the user to the angular page with either a query param or a route param.我对此的思考过程是使用查询参数或路由参数将用户重定向到角度页面。 It's just as secure/unsecure since anyone can see header values (if you're worried about exposing your JWT to the end user.它同样安全/不安全,因为任何人都可以看到标头值(如果您担心将 JWT 暴露给最终用户。

I would make your login page accept a JWT (or whatever your page that's being redirected to is)我会让您的登录页面接受 JWT(或您被重定向到的任何页面)

So, let's say you do https://myangularsite.com/login?jwt=JWTTOKEN所以,假设你做https://myangularsite.com/login?jwt=JWTTOKEN

Then, in your login page, you could grab that JWT Token from the url然后,在您的登录页面中,您可以从 url 中获取该 JWT 令牌

const jwt = ActivatedRoute.snapshot.queryParamMap.get('jwt');

Now, you can put that value in localStorage :现在,您可以将该值放入localStorage

localStorage.setItem('jwt', jwt);

Then, in Angular, just create an HttpInterceptor that grabs that JWT Token from localStorage and applies it to the header for every request:然后,在 Angular 中,只需创建一个HttpInterceptor从 localStorage 中获取 JWT 令牌并将其应用于每个请求的标头:

const jwt = localStorage.getItem('jwt');
const request = req.clone({ withCredentials: true, headers: jwt });

You can kind of think of this as the same thing as when you sign up for some service and they send you a link via email to confirm your email.您可以将其视为与您注册某些服务时相同的事情,他们会通过电子邮件向您发送一个链接以确认您的电子邮件。 That link that you click on takes you back into their site with a Email Confirmation Token.您单击的那个链接会将您带回他们的网站,并带有电子邮件确认令牌。 When you hit that site with that token, it knows who you are from that token.当您使用该令牌访问该站点时,它会通过该令牌知道您是谁。

Ex: http://facebook.com/confirm?token=emailconfirmtoken例如:http://facebook.com/confirm?token= emailconfirmtoken

The above answer is not correct that "It's just as secure/unsecure since anyone can see header values".上面的答案是不正确的,“因为任何人都可以看到标头值,所以它同样安全/不安全”。 Header values are encrypted in transport with TLS;标头值在传输中使用 TLS 加密; whereas, URL query parameters are readable along the entire routing of a request.而 URL 查询参数在请求的整个路由中都是可读的。 If security is a top concern, then do not place anything that could be considered sensitive as a query parameter.如果安全性是最重要的问题,则不要将任何可能被视为敏感的内容作为查询参数。

We couldn't do this achivement only by IIS, finaly we wrote a simple Node.js application as a backend that when you call it, it loads Angular files.我们不能仅通过 IIS 来实现这一目标,最后我们编写了一个简单的 Node.js 应用程序作为后端,当您调用它时,它会加载 Angular 文件。

Site A paases the token to the site B (which is hosting the Node.js backend) in the header through a Post request.站点 A 通过 Post 请求将令牌发送到标头中的站点 B(托管 Node.js 后端)。 Then the node application returns the token in the cookies.So when the angular application loads up, itreads the token from the cookies.然后节点应用程序在 cookie 中返回令牌。因此,当 Angular 应用程序加载时,它会从 cookie 中读取令牌。

At last we put the Node application into the IIS.最后,我们将 Node 应用程序放入 IIS。

Hope it helps other people.希望它可以帮助其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular单页面应用程序(SPA)的Shibboleth身份验证 - Shibboleth authentication for an Angular single page application (SPA) 如何在Ionic 4中发送带有标头的身份验证令牌? - How to send the authentication token with the header in Ionic 4? 将身份验证令牌传递给自定义 Angular 7 库 - Passing authentication token to custom Angular 7 library JWT令牌未在angular6中传入标头 - JWT token is not passing in header in angular6 使用Angular SPA应用程序与IdentityServer4交谈,以获取身份验证/授权,然后与Web API进行通信 - Talking to IdentityServer4 with an Angular SPA application for authentication/authorization receiving a token then talking to Web API SPA RSA 2048认证 - SPA RSA 2048 authentication Asp.net 核心:托管具有相同身份验证的 MVC 和 SPA。 重定向到应用程序的 MVC 部分时客户端被注销 - Asp.net core: hosting MVC and SPA with same Identity authentication. Client gets logged out when redirecting to MVC part of application Oauth 2.0无需访问即可将访问令牌重定向到其他页面 - Oauth 2.0 get access token without redirecting the user to different page 在从 Angular 对 SignalR 集线器的调用中包含身份验证标头(带有访问令牌) - Including authentication header (with access token) in a call to a SignalR hub from Angular 在 Angular 8 的标头中传递正确的令牌请求时被禁止 403, - getting 403 forbidden on passing correct token request in header in Angular 8 ,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM