pub struct SessionAuthenticator { /* private fields */ }Expand description
Session-based authenticator.
Reads user information from the actix-session.
§Spring Security Equivalent
Similar to Spring’s session-based authentication where SecurityContext
is stored in the HttpSession.
§Requirements
SessionMiddlewaremust be configured in your application- User must be logged in via
SessionAuthenticator::login()
§Example
use actix_security_core::http::security::session::{SessionAuthenticator, SessionConfig};
let config = SessionConfig::new()
.fixation_strategy(SessionFixationStrategy::MigrateSession);
let authenticator = SessionAuthenticator::new(config);
// In login handler
async fn login(session: Session, form: Form<LoginForm>) -> impl Responder {
// Validate credentials...
let user = validate_user(&form.username, &form.password)?;
// Store user in session (with fixation protection)
SessionAuthenticator::login(&session, &user, &config)?;
HttpResponse::Ok().body("Logged in")
}Implementations§
Source§impl SessionAuthenticator
impl SessionAuthenticator
Sourcepub fn new(config: SessionConfig) -> SessionAuthenticator
pub fn new(config: SessionConfig) -> SessionAuthenticator
Create a new session authenticator.
Sourcepub fn default_config() -> SessionAuthenticator
pub fn default_config() -> SessionAuthenticator
Create with default configuration.
Sourcepub fn login(
session: &Session,
user: &User,
config: &SessionConfig,
) -> Result<(), SessionError>
pub fn login( session: &Session, user: &User, config: &SessionConfig, ) -> Result<(), SessionError>
Store user in session (login) with session fixation protection.
This method:
- Applies session fixation protection based on configuration
- Stores user data in the session
- Sets the authenticated flag
§Spring Equivalent
Similar to SecurityContextHolder.getContext().setAuthentication(...)
combined with session fixation protection.
§Example
async fn login_handler(
session: Session,
form: Form<LoginForm>,
config: Data<SessionConfig>,
) -> impl Responder {
// Validate credentials
let user = validate_user(&form.username, &form.password)?;
// Login with session fixation protection
SessionAuthenticator::login(&session, &user, &config)?;
HttpResponse::Ok().body("Logged in")
}Sourcepub fn logout(session: &Session, config: &SessionConfig)
pub fn logout(session: &Session, config: &SessionConfig)
Sourcepub fn clear_session(session: &Session)
pub fn clear_session(session: &Session)
Clear entire session (logout + clear all data).
Sourcepub fn is_authenticated(session: &Session, config: &SessionConfig) -> bool
pub fn is_authenticated(session: &Session, config: &SessionConfig) -> bool
Check if session is authenticated.
Sourcepub fn get_session_user(
session: &Session,
config: &SessionConfig,
) -> Option<User>
pub fn get_session_user( session: &Session, config: &SessionConfig, ) -> Option<User>
Get user from session.
Sourcepub fn save_request(
session: &Session,
url: &str,
config: &SessionConfig,
) -> Result<(), SessionError>
pub fn save_request( session: &Session, url: &str, config: &SessionConfig, ) -> Result<(), SessionError>
Save the current request URL for redirect after login.
§Spring Equivalent
Similar to SavedRequest in Spring Security.
Sourcepub fn get_saved_request(
session: &Session,
config: &SessionConfig,
default_url: &str,
) -> String
pub fn get_saved_request( session: &Session, config: &SessionConfig, default_url: &str, ) -> String
Get the saved request URL and remove it from session.
Returns the saved URL or the default URL if none was saved.
Sourcepub fn config(&self) -> &SessionConfig
pub fn config(&self) -> &SessionConfig
Get the configuration.
Trait Implementations§
Source§impl Authenticator for SessionAuthenticator
impl Authenticator for SessionAuthenticator
Source§impl Clone for SessionAuthenticator
impl Clone for SessionAuthenticator
Source§fn clone(&self) -> SessionAuthenticator
fn clone(&self) -> SessionAuthenticator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SessionAuthenticator
impl RefUnwindSafe for SessionAuthenticator
impl Send for SessionAuthenticator
impl Sync for SessionAuthenticator
impl Unpin for SessionAuthenticator
impl UnwindSafe for SessionAuthenticator
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more