JwtBuilder

Struct JwtBuilder 

Source
pub struct JwtBuilder { /* private fields */ }
Expand description

JWT 构建器

使用 Builder 模式创建 JWT token

Implementations§

Source§

impl JwtBuilder

Source

pub fn new() -> Self

创建新的 JWT 构建器

Source

pub fn subject(self, sub: impl Into<String>) -> Self

设置主题(Subject)

通常用于存储用户 ID

Source

pub fn issuer(self, iss: impl Into<String>) -> Self

设置签发者(Issuer)

Source

pub fn audience(self, aud: impl Into<String>) -> Self

设置接收者(Audience)

Source

pub fn expires_in_seconds(self, seconds: i64) -> Self

设置过期时间(秒数,从现在开始)

Source

pub fn expires_in_minutes(self, minutes: i64) -> Self

设置过期时间(分钟数,从现在开始)

Source

pub fn expires_in_hours(self, hours: i64) -> Self

设置过期时间(小时数,从现在开始)

Source

pub fn expires_in_days(self, days: i64) -> Self

设置过期时间(天数,从现在开始)

Source

pub fn expires_in(self, duration: Duration) -> Self

设置过期时间(Duration)

Source

pub fn not_before(self, nbf: i64) -> Self

设置生效时间(Not Before)

Source

pub fn not_before_in_seconds(self, seconds: i64) -> Self

设置延迟生效(秒数,从现在开始)

Source

pub fn jwt_id(self, jti: impl Into<String>) -> Self

设置 JWT ID

Source

pub fn with_random_jwt_id(self) -> Self

自动生成 JWT ID

Source

pub fn claim<V: Serialize>(self, key: impl Into<String>, value: V) -> Self

添加自定义字段

Source

pub fn algorithm(self, algorithm: JwtAlgorithm) -> Self

设置算法

Source

pub fn build_with_secret(self, secret: &[u8]) -> Result<String>

使用密钥构建 JWT(适用于 HMAC 算法)

§参数
  • secret - 用于签名的密钥
§返回

返回编码后的 JWT 字符串

Source

pub fn build_with_rsa_private_key( self, private_key_pem: &[u8], ) -> Result<String>

使用 RSA 私钥构建 JWT

§参数
  • private_key_pem - PEM 格式的 RSA 私钥
Source

pub fn build_with_ec_private_key(self, private_key_pem: &[u8]) -> Result<String>

使用 EC 私钥构建 JWT

§参数
  • private_key_pem - PEM 格式的 EC 私钥
Source

pub fn get_claims(&self) -> &Claims

获取 claims 引用(用于调试)

Trait Implementations§

Source§

impl Clone for JwtBuilder

Source§

fn clone(&self) -> JwtBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JwtBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JwtBuilder

Source§

fn default() -> JwtBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V