pub struct Session { /* private fields */ }Expand description
HTTP Session,持有 Redis 缓存引用、配置参数和当前会话数据。
实现了 axum FromRequestParts,可直接作为 handler 参数提取。
Implementations§
Source§impl Session
impl Session
Sourcepub fn new(cache: &'static RedisCache, params: Arc<SessionParams>) -> Self
pub fn new(cache: &'static RedisCache, params: Arc<SessionParams>) -> Self
创建未登录的空 Session,数据从下一次请求中按需加载。
Sourcepub fn with_account(self, account: impl Into<String>, user_id: i64) -> Self
pub fn with_account(self, account: impl Into<String>, user_id: i64) -> Self
设置账号和用户 ID,账号变更时自动生成新的 Session ID,支持链式调用。
Sourcepub fn with_roles(self, roles: Vec<String>) -> Self
pub fn with_roles(self, roles: Vec<String>) -> Self
设置角色列表,支持链式调用。
Sourcepub fn with_groups(self, groups: Vec<String>) -> Self
pub fn with_groups(self, groups: Vec<String>) -> Self
设置用户组列表,支持链式调用。
Sourcepub fn get_account(&self) -> &str
pub fn get_account(&self) -> &str
返回当前登录的用户账号。
Sourcepub fn get_user_id(&self) -> i64
pub fn get_user_id(&self) -> i64
返回当前登录的用户 ID。
Sourcepub fn get_expired_at(&self) -> String
pub fn get_expired_at(&self) -> String
返回 Session 过期时间的格式化字符串。
Sourcepub fn is_will_expired(&self) -> bool
pub fn is_will_expired(&self) -> bool
返回 true 表示 Session 将在 1 小时内过期。
Sourcepub fn get_issued_at(&self) -> String
pub fn get_issued_at(&self) -> String
返回 Session 签发时间的格式化字符串。
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
返回 true 表示 Session 已超过 TTL 过期。
Trait Implementations§
Source§impl From<AdminSession> for Session
impl From<AdminSession> for Session
Source§fn from(se: AdminSession) -> Self
fn from(se: AdminSession) -> Self
Converts to this type from the input type.
Source§impl From<UserSession> for Session
impl From<UserSession> for Session
Source§fn from(se: UserSession) -> Self
fn from(se: UserSession) -> Self
Converts to this type from the input type.
Source§impl<S> FromRequestParts<S> for Session
axum extractor:从请求扩展中提取 Session,按需从 Redis 加载数据。
若 Cookie 中存在有效 Session ID 且 Redis 中有对应数据,则填充 SessionData。
impl<S> FromRequestParts<S> for Session
axum extractor:从请求扩展中提取 Session,按需从 Redis 加载数据。 若 Cookie 中存在有效 Session ID 且 Redis 中有对应数据,则填充 SessionData。
Source§impl IntoResponse for Session
将 Session 序列化为 HTTP 响应:设置签名 Cookie + JSON 账号信息。
impl IntoResponse for Session
将 Session 序列化为 HTTP 响应:设置签名 Cookie + JSON 账号信息。
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Source§impl TryFrom<&Session> for SignedCookieJar
将 Session 转换为携带签名 Cookie 的 SignedCookieJar。
Session ID 为空时将 Cookie max-age 设为 0(即删除 Cookie)。
impl TryFrom<&Session> for SignedCookieJar
将 Session 转换为携带签名 Cookie 的 SignedCookieJar。
Session ID 为空时将 Cookie max-age 设为 0(即删除 Cookie)。
Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
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>>
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
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
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>>,
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 moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Convert the handler into a
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
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, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
Convert the handler into a
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
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