pub struct WebSocketSecurityConfig { /* private fields */ }Expand description
Configuration for WebSocket security.
This provides a unified way to configure security for WebSocket endpoints, combining authentication requirements and origin validation.
§Spring Security Equivalent
WebSocketSecurityConfigurer / AbstractSecurityWebSocketMessageBrokerConfigurer
§Example
ⓘ
use actix_security::http::security::websocket::WebSocketSecurityConfig;
// Create configuration
let ws_config = WebSocketSecurityConfig::new()
.allowed_origins(vec!["https://myapp.com".into()])
.require_authentication(true)
.required_roles(vec!["USER".into()]);
// Use in handler
#[get("/ws")]
async fn ws_handler(
req: HttpRequest,
stream: web::Payload,
config: web::Data<WebSocketSecurityConfig>,
) -> Result<HttpResponse, actix_web::Error> {
let upgrade = config.validate_upgrade(&req)?;
// ... upgrade to WebSocket
}Implementations§
Source§impl WebSocketSecurityConfig
impl WebSocketSecurityConfig
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new WebSocket security configuration with default settings.
Default settings:
- No origin validation (allow any)
- Authentication not required
- No role/authority requirements
Sourcepub fn allowed_origins(self, origins: Vec<String>) -> Self
pub fn allowed_origins(self, origins: Vec<String>) -> Self
Sourcepub fn origin_validator(self, validator: OriginValidator) -> Self
pub fn origin_validator(self, validator: OriginValidator) -> Self
Sourcepub fn require_authentication(self, require: bool) -> Self
pub fn require_authentication(self, require: bool) -> Self
Sourcepub fn required_roles(self, roles: Vec<String>) -> Self
pub fn required_roles(self, roles: Vec<String>) -> Self
Sourcepub fn validate_upgrade(
&self,
req: &HttpRequest,
) -> Result<WebSocketUpgrade, WebSocketSecurityError>
pub fn validate_upgrade( &self, req: &HttpRequest, ) -> Result<WebSocketUpgrade, WebSocketSecurityError>
Validates a WebSocket upgrade request.
This method performs all configured security checks:
- Origin validation (CSWSH prevention)
- Authentication check (if required)
- Role check (if configured)
- Authority check (if configured)
§Returns
Ok(WebSocketUpgrade)- Validation passed, safe to upgradeErr(WebSocketSecurityError)- Validation failed
§Example
ⓘ
let config = WebSocketSecurityConfig::new()
.allowed_origins(vec!["https://myapp.com".into()])
.require_authentication(true);
#[get("/ws")]
async fn ws_handler(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let upgrade = config.validate_upgrade(&req)?;
let user = upgrade.into_user().unwrap();
// ... upgrade to WebSocket
}Trait Implementations§
Source§impl Clone for WebSocketSecurityConfig
impl Clone for WebSocketSecurityConfig
Source§fn clone(&self) -> WebSocketSecurityConfig
fn clone(&self) -> WebSocketSecurityConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for WebSocketSecurityConfig
impl Debug for WebSocketSecurityConfig
Auto Trait Implementations§
impl Freeze for WebSocketSecurityConfig
impl RefUnwindSafe for WebSocketSecurityConfig
impl Send for WebSocketSecurityConfig
impl Sync for WebSocketSecurityConfig
impl Unpin for WebSocketSecurityConfig
impl UnwindSafe for WebSocketSecurityConfig
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<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