jsonwebtoken
Rust实现的JSON Web Token库,用于安全身份验证。
安装
将以下内容加入 Cargo.toml:
jsonwebtoken = "7"
serde = {version = "1.0", features = ["derive"] }
需要Rust 1.39及以上版本
算法
这个库目前支持以下算法:
HS256
HS384
HS512
RS256
RS384
RS512
PS256
PS384
PS512
ES256
ES384
如何使用
引用与结构型:
use serde::{Serialize, Deserialize};
use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey};
/// 我们的声言结构型, 需要由`Serialize` 或 `Deserialize`派生
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
company: String,
exp: usize,
}
声言
声言中可被验证的字段。
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
aud: String, // 可选。听众
exp: usize, // 必须。(validate_exp 在验证中默认为真值)。截止时间 (UTC 时间戳)
iat: