java - 服务端如何返回 JWT token 给客户端?

标签 java authentication oauth-2.0 http-headers jwt

这是我第一次接触 JWT token ,我想知道这个 token 在首次创建后如何返回给客户端。

它应该出现在 Authorization : Bearer header 中吗?

通常,客户端在每次请求时传递 Authorization : Bearer header 中的 token 。
我想知道在用户通过身份验证并创建 token 后,服务器如何将此 token 传递给客户端。也在同一个标​​题中?在不同的标题中?

在我的情况下,服务器将生成 token 而不是作为响应而是作为请求的一部分。

例如:-

用户将登录到门户,然后单击指向授权应用程序的链接。包含用户声明的 JWT 将作为请求的一部分传递给授权应用程序。
这里最好的方法是什么?获取还是发布?标题(哪个)?请求参数?发布正文? 谢谢!

最佳答案

没有关于如何将 JWT token 返回给客户端的标准,但是,检查这个 URL,它回答了你的问题

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. For a REST-only App/API you are free to send the JWT as the response body or a 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) 👍

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. see: 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

关于java - 服务端如何返回 JWT token 给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503024/

相关文章:

java - EJB3.1 TimerService @Timeout 当前主体

google-app-engine - App Engine 作为 OAuth2 提供者

security - 客户端和用户代理之间的区别

java - {Facebook 登录} java.lang.NoClassDefFoundError : android. support.v4.content.LocalBroadcastManager

javax.servlet.ServletException : java. lang.LinkageError

2 个客户端之间的 Java 套接字本地连接 - 设计

JSF 身份验证注销

java - 如何在 JavaFX 中交换父节点的子节点?

ruby - Meteor 使用 bcrypt 验证来自不同服务器的电子邮件/密码

oauth-2.0 - 具有密码授予功能的 DotNetOpenAuth OAuth 2.0 授权服务器