Skip to main content

Crate actix_security

Crate actix_security 

Source
Expand description

§Actix Security

Spring Security-inspired authentication and authorization for Actix Web.

This crate provides a unified API combining:

  • actix-security-core: Security middleware, authentication, and authorization
  • actix-security-codegen: Procedural macros (#[secured], #[pre_authorize], etc.)

§Quick Start

Add to your Cargo.toml:

[dependencies]
actix-web = "4"
actix-security = { version = "0.2", features = ["argon2", "http-basic"] }

§Example

use actix_web::{get, App, HttpServer, HttpResponse, Responder};
use actix_security::{secured, pre_authorize};
use actix_security::http::security::{
    AuthenticatedUser, AuthenticationManager, AuthorizationManager,
    Argon2PasswordEncoder, PasswordEncoder, User,
};
use actix_security::http::security::middleware::SecurityTransform;

#[secured("ADMIN")]
#[get("/admin")]
async fn admin(user: AuthenticatedUser) -> impl Responder {
    HttpResponse::Ok().body(format!("Welcome, Admin {}!", user.get_username()))
}

#[pre_authorize("hasRole('USER') AND hasAuthority('posts:write')")]
#[post("/posts")]
async fn create_post(user: AuthenticatedUser) -> impl Responder {
    HttpResponse::Created().body("Post created")
}

§Features

FeatureDefaultDescription
macrosYesProcedural macros (#[secured], #[pre_authorize], etc.)
argon2YesArgon2 password encoder
http-basicYesHTTP Basic authentication
jwtNoJWT authentication (with RSA support)
sessionNoSession-based authentication with fixation protection
form-loginNoForm-based login with redirect support
remember-meNoRemember-me persistent authentication
csrfNoCSRF protection middleware
oauth2NoOAuth2/OIDC authentication
user-detailsNoAsync UserDetailsService trait
fullNoAll features enabled

§Modules

The main functionality is available through the http module:

Modules§

http
prelude
Prelude module for convenient imports

Attribute Macros§

authenticated
Deprecated: Use #[pre_authorize(authenticated)] instead.
deny_all
Marks an endpoint as completely inaccessible (always returns 403 Forbidden).
has_access
Deprecated: Use #[pre_authorize(authority = "...")] instead.
has_role
Deprecated: Use #[secured("ROLE")] instead.
permit_all
Marks an endpoint as publicly accessible (no authentication required).
pre_authorize
Flexible method security annotation with SpEL-like expressions.
roles_allowed
Role-based method security annotation (Java EE standard).
secured
Role-based method security annotation.