我需要在哪里使用 JWT? [英] Where do I need to use JWT?

查看:26
本文介绍了我需要在哪里使用 JWT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 结构和协议之外,我是想知道 JWT 适合客户端/服务器通信吗?

The structure and protocol aside, I was wondering where JWT fits into client/server communication?

  • 这里是用来代替身份验证和会话 cookie 的吗?
  • 这是为了减轻服务器在数据库或内存中存储会话令牌的负担吗?
  • 客户端是否需要确保他们从预期的服务器接收数据,如果这不是问题,我就不需要 JWT?
  • 当连接是 HTTPS/SSL 时,服务器到服务器的通信是否必要或良好的做法?

推荐答案

JWT到底是什么?

它是一个只有服务器才能生成的令牌,并且可以包含数据的有效负载.

What JWT is exactly?

It is a token that only the server can generate, and can contain a payload of data.

JWT 有效负载可以包含用户 ID 等内容,因此当客户端向您发送 JWT 时,您可以确保它是由您发出的,并且您可以查看它发给谁已发布.

A JWT payload can contain things like user ID so that when the client sends you a JWT, you can be sure that it is issued by you, and you can see to whom it was issued.

通常,在 RESTful APIs 中,服务器不得使用任何类型的会议.

Usually, in RESTful APIs, where the server must not use any sort of sessions.

  • 在典型的会话流中,浏览器发送一个包含令牌的 cookie,该令牌然后在服务器上匹配到服务器使用的某些数据对用户进行身份验证.

  • In a typical session flow, the browser sends a cookie containing a token, which is then matched at the server to some data which the server makes use of to authenticate the user.

在 JWT 流中,令牌本身包含数据.服务器解码令牌以仅对用户进行身份验证.服务器上不存储任何数据.

In a JWT flow, the token itself contains the data. The server decodes the token to authenticate the user only. No data stored on the server.

  • 发送到 /signin
  • 的用户凭据
  • /signin 返回一个 JWT
  • JWT 存储在 localStorage
  • 每次请求都会发送 JWT(发送到 API?)
  • 服务器解密 JWT 并从中提取用户 ID
  • 服务器根据经过身份验证的用户发送响应.

这篇关于我需要在哪里使用 JWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆