pub struct SecurityContext;Expand description
Holder for the current security context.
§Spring Security Equivalent
SecurityContextHolder
Provides static methods to access the current authenticated user from anywhere in the application.
Implementations§
Source§impl SecurityContext
impl SecurityContext
Sourcepub fn get_user() -> Option<User>
pub fn get_user() -> Option<User>
Gets the current authenticated user.
§Spring Security Equivalent
SecurityContextHolder.getContext().getAuthentication().getPrincipal()§Returns
Some(User)if a user is authenticated in the current contextNoneif no user is authenticated
§Example
ⓘ
use actix_security_core::http::security::context::SecurityContext;
if let Some(user) = SecurityContext::get_user() {
println!("Current user: {}", user.get_username());
}Sourcepub fn get_username() -> Option<String>
pub fn get_username() -> Option<String>
Sourcepub fn is_authenticated() -> bool
pub fn is_authenticated() -> bool
Checks if the current user is authenticated.
§Spring Security Equivalent
SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
Sourcepub fn has_any_role(roles: &[&str]) -> bool
pub fn has_any_role(roles: &[&str]) -> bool
Checks if the current user has any of the specified roles.
Checks if the current user has any of the specified authorities.
Sourcepub async fn run_with<F, R>(user: Option<User>, f: F) -> Rwhere
F: Future<Output = R>,
pub async fn run_with<F, R>(user: Option<User>, f: F) -> Rwhere
F: Future<Output = R>,
Runs a closure with the given user set in the security context.
§Spring Security Equivalent
SecurityContextHolder.setContext(context)
This is primarily used internally by the security middleware.
§Example
ⓘ
let user = User::new("admin".into(), "password".into());
SecurityContext::run_with(Some(user), async {
// Code here can access SecurityContext::get_user()
let username = SecurityContext::get_username();
}).await;Auto Trait Implementations§
impl Freeze for SecurityContext
impl RefUnwindSafe for SecurityContext
impl Send for SecurityContext
impl Sync for SecurityContext
impl Unpin for SecurityContext
impl UnwindSafe for SecurityContext
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> 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>
Converts
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>
Converts
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