简体   繁体   English

服务器如何将 JWT 令牌返回给客户端?

[英]How does server return JWT token to the client?

This is my first encounter with a JWT token and I'd like to know how is this token returned to the client after it's first created.这是我第一次遇到 JWT 令牌,我想知道该令牌在首次创建后如何返回给客户端。

Should it come in the Authorization : Bearer header ?它应该出现在Authorization : Bearer header吗?

Usually, it's the client that passes the token in Authorization : Bearer header on each request.通常,是客户端在每个请求的Authorization : Bearer header中传递令牌。
I'd like to know how does the server pass this token to the client after user has authenticated and the token gets created.我想知道在用户通过身份验证并创建令牌后,服务器如何将此令牌传递给客户端。 Also in the same header?也在同一个标​​题中? In a different header?在不同的标题中?

In my situation, the server will be generating the token not as a response but as part of the request.在我的情况下,服务器将生成令牌而不是作为响应,而是作为请求的一部分。

For example:-例如:-

A user will login to a portal, then click on a link to an authorized application.用户将登录到门户,然后单击指向授权应用程序的链接。 The JWT containing user claims will be passed to the authorized application as part of the request.包含用户声明的 JWT 将作为请求的一部分传递给授权应用程序。
What is the best approach here?这里最好的方法是什么? GET or POST?获取还是发布? Header (which)?标题(哪个)? Query string?请求参数? POST body? POST 身体? Thank you!谢谢!

there is no standard for how to return JWT token to the client, however, check this URL, it answers your question没有关于如何将 JWT 令牌返回给客户端的标准,但是,请检查此 URL,它可以回答您的问题

https://github.com/dwyl/hapi-auth-jwt2/issues/82#issuecomment-129873082 https://github.com/dwyl/hapi-auth-jwt2/issues/82#issuecomment-129873082

putting the JWT token in the Authorization header gives us flexibility to send an actual response in a web application.将 JWT 令牌放在 Authorization 标头中使我们可以灵活地在 Web 应用程序中发送实际响应。 For a REST-only App/API you are free to send the JWT as the response body or a cookie.对于仅 REST 的应用程序/API,您可以自由地将 JWT 作为响应正文或 cookie 发送。 What matters is how the client stores the JWT and sends it back to the Server, which is done in the Authorization header (or Cookie or URL Token if you prefer) 👍重要的是客户端如何存储 JWT 并将其发送回服务器,这在 Authorization 标头(或 Cookie 或 URL 令牌,如果您愿意)中完成👍

As for this existing in the "wild", I have not seen an example of the server sending an Authorisation header to the client, but there is nothing in the spec to suggest this is an anti-pattern.至于存在于“wild”中的这种情况,我还没有看到服务器向客户端发送 Authorization 标头的示例,但规范中没有任何内容表明这是一种反模式。 see: http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html见: http : //self-issued.info/docs/draft-ietf-oauth-v2-bearer.html

If you want to stick to the guidelines you would do follow this example: http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#ExAccTokResp如果您想遵守指南,您可以按照以下示例进行操作: http : //self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#ExAccTokResp

One may be interested to know that the OAuth 2.0 standard specifies the response body for that purpose:人们可能有兴趣知道OAuth 2.0 标准为此目的指定了响应主体:

5.1. 5.1. Successful Response成功响应

The authorization server issues an access token and optional refresh token, and constructs the response by adding the following parameters to the entity-body of the HTTP response with a 200 (OK) status code:授权服务器发出访问令牌和可选的刷新令牌,并通过将以下参数添加到带有 200(OK)状态代码的 HTTP 响应的实体主体中来构造响应:

access_token
REQUIRED.必需的。 The access token issued by the authorization server.授权服务器颁发的访问令牌。
[...] [...]

Also, there is another strategy when you can put token in the url.此外,当您可以将令牌放入 url 时,还有另一种策略。 On the server side you can add the token after the url in case you have redirection from some security service.在服务器端,您可以在 url 之后添加令牌,以防您从某些安全服务重定向 For example:例如:

http://[my-app]/index.html?access_token=sadmopwmopmdmvsasom....

And then in js you can get it like this:然后在 js 中你可以这样得到它:

let url = new URL(window.location);
let accessToken = url.searchParams.get("access_token"); 

But , keep in mind that this method can't be considered as safe.但是,请记住,这种方法不能被认为是安全的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM