pgjwt:JSON 网页令牌
Supabase 会为你创建并管理 JWT。这功能是内置在平台里的。如果你使用的是 15 版或更早的 Postgres,你不需要 pgjwt 扩展,可以安全地禁用它。想了解更多关于 Supabase 如何处理 JWT 的信息,可以阅读 Supabase 和 JWT 文档
🌐 Supabase creates and handles JWT for you. It is built into the platform. If you use Postgres version 15 or earlier, you don't need the pgjwt extension, and it is safe to disable. For more information on how Supabase handles JWTs, read the Supabase and JWTs documentation
在使用 Postgres 17 的项目中,pgjwt 扩展已被弃用。在使用 Postgres 15 的项目中仍然支持,但在将这些项目升级到 Postgres 17 之前需要先移除它。更多信息请参见 升级到 Postgres 17 说明。
🌐 The pgjwt extension is deprecated in projects using Postgres 17. It continues to be supported in projects using Postgres 15, but will need to dropped before those projects are upgraded to Postgres 17. See the Upgrading to Postgres 17 notes for more information.
pgjwt(Postgres JSON Web Token)扩展允许你在 Postgres 数据库中创建和解析 JSON Web Tokens (JWTs)。JWT 通常用于 Web 应用和服务中的身份验证和授权。
🌐 The pgjwt (Postgres JSON Web Token) extension allows you to create and parse JSON Web Tokens (JWTs) within a Postgres database. JWTs are commonly used for authentication and authorization in web applications and services.
启用扩展 #
🌐 Enable the extension
- 在仪表板中转到数据库页面。
- 点击侧边栏的 扩展。
- 搜索
pgjwt并启用这个扩展。
应用接口 #
🌐 API
sign(payload json, secret text, algorithm text default 'HS256'):使用 algorithm 和 secret 对包含 payload 的 JWT 进行签名。verify(token text, secret text, algorithm text default 'HS256'):使用 algorithm 解码用 secret 签名的 JWT token。
在哪里:
🌐 Where:
payload是一个以字符串表示的加密 JWT。secret是用于签署 JWT 并验证其完整性的私有/秘密通行码。algorithm是用来使用密钥对 JWT 进行签名的方法。token是一个以字符串表示的加密 JWT。
用法 #
🌐 Usage
一旦安装了这个扩展,你就可以使用它的功能来创建和解析 JWT。下面是一个使用 sign 函数创建 JWT 的示例:
🌐 Once the extension is installed, you can use its functions to create and parse JWTs. Here's an example of how you can use the sign function to create a JWT:
1select2 extensions.sign(3 payload := '{"sub":"1234567890","name":"John Doe","iat":1516239022}',4 secret := 'secret',5 algorithm := 'HS256'6 );pgjwt_encode 函数返回一个表示 JWT 的字符串,然后可以在各方之间安全传输。
🌐 The pgjwt_encode function returns a string that represents the JWT, which can then be safely transmitted between parties.
1sign2---------------------------------3 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpX4 VCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiw5 ibmFtZSI6IkpvaG4gRG9lIiwiaWF0Ijo6 xNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y97 22BhjWgQzWXcXNrz0ogtVhfEd2o8(1 row)要解析 JWT 并提取其声明,你可以使用 verify 函数。下面是一个例子:
🌐 To parse a JWT and extract its claims, you can use the verify function. Here's an example:
1select2 extensions.verify(3 token := 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiRm9vIn0.Q8hKjuadCEhnCPuqIj9bfLhTh_9QSxshTRsA5Aq4IuM',4 secret := 'secret',5 algorithm := 'HS256'6 );它返回解码后的内容以及一些相关的元数据。
🌐 Which returns the decoded contents and some associated metadata.
1header | payload | valid2-----------------------------+----------------+-------3 {"alg":"HS256","typ":"JWT"} | {"name":"Foo"} | t4(1 row)资源 #
🌐 Resources
- 官方
pgjwt文档