Expand description
Security Context for accessing the current authenticated user.
§Spring Security Equivalent
org.springframework.security.core.context.SecurityContextHolder
§Overview
The SecurityContext provides access to the current authenticated user from anywhere in your application, not just from request handlers.
§Usage
ⓘ
use actix_security_core::http::security::context::SecurityContext;
// In a service layer function
fn get_current_username() -> Option<String> {
SecurityContext::get_user()
.map(|user| user.get_username().to_string())
}
// Check if current user has a role
fn is_admin() -> bool {
SecurityContext::get_user()
.map(|user| user.has_role("ADMIN"))
.unwrap_or(false)
}§Thread Safety
The context uses task-local storage, making it safe for async code. Each async task has its own isolated security context.
Structs§
- Security
Context - Holder for the current security context.
- Security
Context Guard - Guard that clears the security context when dropped.