Skip to main content

Session

Struct Session 

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

HTTP Session,持有 Redis 缓存引用、配置参数和当前会话数据。 实现了 axum FromRequestParts,可直接作为 handler 参数提取。

Implementations§

Source§

impl Session

Source

pub fn new(cache: &'static RedisCache, params: Arc<SessionParams>) -> Self

创建未登录的空 Session,数据从下一次请求中按需加载。

Source

pub fn is_login(&self) -> bool

返回 true 表示用户已登录(account 非空)。

Source

pub fn can_renew(&self) -> bool

返回 true 表示 Session 尚未达到最大续期次数,可以续期。

Source

pub fn with_account(self, account: impl Into<String>, user_id: i64) -> Self

设置账号和用户 ID,账号变更时自动生成新的 Session ID,支持链式调用。

Source

pub fn with_roles(self, roles: Vec<String>) -> Self

设置角色列表,支持链式调用。

Source

pub fn with_groups(self, groups: Vec<String>) -> Self

设置用户组列表,支持链式调用。

Source

pub fn refresh(&mut self)

续期:累加续期计数并更新签发时间戳。

Source

pub fn get_account(&self) -> &str

返回当前登录的用户账号。

Source

pub fn get_user_id(&self) -> i64

返回当前登录的用户 ID。

Source

pub fn get_expired_at(&self) -> String

返回 Session 过期时间的格式化字符串。

Source

pub fn is_will_expired(&self) -> bool

返回 true 表示 Session 将在 1 小时内过期。

Source

pub fn get_issued_at(&self) -> String

返回 Session 签发时间的格式化字符串。

Source

pub fn is_expired(&self) -> bool

返回 true 表示 Session 已超过 TTL 过期。

Source

pub fn reset(&mut self)

重置 Session(登出),清除 ID 和账号信息。

Source

pub async fn save(&self) -> Result<(), Error>

将当前 Session 数据持久化到 Redis,TTL 与配置一致。

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl From<AdminSession> for Session

Source§

fn from(se: AdminSession) -> Self

Converts to this type from the input type.
Source§

impl From<UserSession> for Session

Source§

fn from(se: UserSession) -> Self

Converts to this type from the input type.
Source§

impl<S> FromRequestParts<S> for Session
where S: Send + Sync,

axum extractor:从请求扩展中提取 Session,按需从 Redis 加载数据。 若 Cookie 中存在有效 Session ID 且 Redis 中有对应数据,则填充 SessionData。

Source§

type Rejection = Error

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl IntoResponse for Session

将 Session 序列化为 HTTP 响应:设置签名 Cookie + JSON 账号信息。

Source§

fn into_response(self) -> Response

Create a response.
Source§

impl TryFrom<&Session> for SignedCookieJar

将 Session 转换为携带签名 Cookie 的 SignedCookieJar。 Session ID 为空时将 Cookie max-age 设为 0(即删除 Cookie)。

Source§

type Error = Error

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

fn try_from(se: &Session) -> Result<Self, Self::Error>

Performs the conversion.

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<T, S> Handler<IntoResponseHandler, S> for T
where T: IntoResponse + Clone + Send + Sync + 'static,

Source§

type Future = Ready<Response<Body>>

The type of future calling this handler returns.
Source§

fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future

Call the handler with the given request.
Source§

fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>
where L: Layer<HandlerService<Self, T, S>> + Clone, <L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,

Apply a tower::Layer to the handler. Read more
Source§

fn with_state(self, state: S) -> HandlerService<Self, T, S>

Convert the handler into a Service by providing the state
Source§

impl<H, T> HandlerWithoutStateExt<T> for H
where H: Handler<T, ()>,

Source§

fn into_service(self) -> HandlerService<H, T, ()>

Convert the handler into a Service and no state.
Source§

fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>

Convert the handler into a MakeService and no state. Read more
Source§

fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>

Convert the handler into a MakeService which stores information about the incoming connection and has no state. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,