Skip to main content

JwtService

Struct JwtService 

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

JWT 服务

Implementations§

Source§

impl JwtService

Source

pub fn new(config: JwtConfig) -> JwtResult<Self>

创建新的 JWT 服务

§Arguments
  • config - JWT 配置
Source

pub fn generate_token<T: Serialize>(&self, claims: &T) -> JwtResult<String>

生成令牌

§Arguments
  • claims - JWT Claims
Source

pub fn verify_token<T: DeserializeOwned + 'static>( &self, token: &str, ) -> JwtResult<T>

验证令牌

§Arguments
  • token - JWT 令牌
Source

pub fn generate_access_token( &self, user_id: &str, claims: AccessTokenClaims, ) -> JwtResult<String>

生成访问令牌

§Arguments
  • user_id - 用户 ID
  • claims - 访问令牌 Claims
Source

pub fn verify_access_token(&self, token: &str) -> JwtResult<JwtClaims>

验证访问令牌

§Arguments
  • token - 访问令牌
Source

pub fn generate_refresh_token( &self, user_id: &str, session_id: &str, version: u32, ) -> JwtResult<String>

生成刷新令牌

§Arguments
  • user_id - 用户 ID
  • session_id - 会话 ID
  • version - 令牌版本
Source

pub fn verify_refresh_token(&self, token: &str) -> JwtResult<RefreshTokenClaims>

验证刷新令牌

§Arguments
  • token - 刷新令牌
Source

pub fn decode_unchecked<T: DeserializeOwned>(&self, token: &str) -> JwtResult<T>

解码令牌(不验证签名)

§Arguments
  • token - JWT 令牌
Source

pub fn get_remaining_ttl(&self, token: &str) -> JwtResult<i64>

获取令牌剩余有效期

§Arguments
  • token - JWT 令牌
Source

pub fn is_token_expiring_soon( &self, token: &str, threshold_seconds: i64, ) -> JwtResult<bool>

检查令牌是否即将过期

§Arguments
  • token - JWT 令牌
  • threshold_seconds - 阈值(秒)
Source

pub fn config(&self) -> &JwtConfig

获取配置

Source§

impl JwtService

Source

pub fn generate_token_pair( &self, user_id: &str, access_claims: AccessTokenClaims, session_id: &str, ) -> JwtResult<TokenPair>

生成令牌对

§Arguments
  • user_id - 用户 ID
  • access_claims - 访问令牌 Claims
  • session_id - 会话 ID
Source

pub fn rotate_token_pair( &self, refresh_token: &str, new_access_claims: Option<AccessTokenClaims>, ) -> JwtResult<TokenPair>

轮换刷新令牌

使用旧的刷新令牌生成新的令牌对,并递增版本号

§Arguments
  • refresh_token - 旧的刷新令牌
  • new_access_claims - 新的访问令牌 Claims(可选)

Trait Implementations§

Source§

impl Clone for JwtService

Source§

fn clone(&self) -> JwtService

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 JwtService

Source§

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

Formats the value using the given formatter. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more