actix-security-codegen-0.2.0 has been yanked.
Actix Security
Spring Security-inspired authentication and authorization for Actix Web.
Actix Security brings the power and familiarity of Spring Security to Rust, providing a comprehensive, declarative approach to securing your web applications.
Features
Core Security
- Declarative Security - Attribute macros like
#[secured],#[pre_authorize],#[permit_all] - Expression Language - Write rules like
hasRole('ADMIN') OR hasAuthority('users:write') - Compile-Time Validation - Security expressions are validated at build time
- Pluggable Architecture - Easy to extend with custom authenticators and authorizers
Authentication Methods
- HTTP Basic - Standard HTTP Basic authentication (RFC 7617)
- JWT - Stateless token-based authentication with RSA support and refresh tokens
- Session - Server-side session management with session fixation protection
- Form Login - Spring-like form-based authentication with redirect support
- Remember-Me - Persistent login functionality
- OAuth2 / OIDC - Social login (Google, GitHub, etc.) and enterprise SSO
- LDAP - LDAP/Active Directory authentication
- SAML 2.0 - Enterprise Single Sign-On with support for Okta, Azure AD, ADFS
Security Features
- CSRF Protection - Token-based CSRF protection middleware
- Rate Limiting - Brute-force protection with configurable algorithms (Fixed Window, Sliding Window, Token Bucket)
- Account Locking - Automatic account lockout after failed attempts with progressive delays
- Audit Logging - Security event logging with JSON support
- Security Headers - Built-in middleware for CSP, HSTS, X-Frame-Options, etc.
- Channel Security - HTTPS enforcement and redirect
Password Encoding
- Argon2 - Recommended password hashing algorithm
- BCrypt - Compatible with existing BCrypt hashes
- Delegating Encoder - Automatic encoder detection from hash prefix
Utilities
- AntMatcher - Spring-style URL pattern matching (
/api/**,/users/*/profile) - UserDetailsService - Async trait for loading users from any source
- Security Context - Access current user from anywhere
Quick Start
Add dependencies to your Cargo.toml:
[]
= "4"
= { = "0.2", = ["argon2", "http-basic"] }
Create a secured application:
use ;
use ;
use ;
use SecurityTransform;
async
async
async
Security Macros
| Macro | Spring Equivalent | Description |
|---|---|---|
#[secured("ADMIN")] |
@Secured("ROLE_ADMIN") |
Role-based access |
#[pre_authorize("...")] |
@PreAuthorize("...") |
Expression-based access |
#[permit_all] |
@PermitAll |
Public access |
#[deny_all] |
@DenyAll |
Deny all access |
#[roles_allowed("ADMIN")] |
@RolesAllowed("ADMIN") |
Java EE style |
Expression Language
// Role checks
// Authority checks
// Logical operators
// Complex expressions
URL-Based Authorization
use ;
request_matcher
.login_url
.http_basic
.add_matcher
.add_matcher
.add_matcher
Rate Limiting
use ;
use Duration;
let rate_limiter = new;
new
.wrap
Account Locking
use ;
use Duration;
let lock_manager = new;
// Check before login
let result = check_login.await;
if !result.is_allowed
// Record failure
lock_manager.record_failure.await;
// Record success (resets counter)
lock_manager.record_success.await;
Audit Logging
use ;
let logger = new
.add_handler;
// Log security events
logger.log_login_success;
logger.log_login_failure;
logger.log;
Security Headers
use SecurityHeaders;
new
.wrap // Safe defaults
// or
.wrap // Maximum security
Documentation
- User Guide - Comprehensive documentation
- API Docs - Detailed API reference
- Examples - Working examples
Documentation Chapters
- Getting Started
- Authentication
- Authorization
- Security Macros
- Expression Language
- Security Headers
- Advanced Topics
Examples
All examples are in the examples/ directory with individual README files.
# Run any example
| Example | Description | Features |
|---|---|---|
basic_auth |
HTTP Basic authentication | http-basic, argon2 |
jwt_auth |
JWT token authentication | jwt |
session_auth |
Session-based authentication | session |
form_login |
Form-based login with CSRF | form-login, csrf |
security_headers |
Security HTTP headers | (core) |
oidc_keycloak |
OAuth2/OIDC with Keycloak | oauth2 |
security_complete |
All features combined | full |
Feature Flags
| Feature | Default | Description |
|---|---|---|
macros |
Yes | Procedural macros (#[secured], #[pre_authorize], etc.) |
argon2 |
Yes | Argon2 password encoder |
http-basic |
Yes | HTTP Basic authentication |
bcrypt |
No | BCrypt password encoder |
jwt |
No | JWT authentication (HS256, RS256, ES256) |
session |
No | Session-based authentication |
form-login |
No | Form-based login |
csrf |
No | CSRF protection middleware |
remember-me |
No | Remember-me authentication |
oauth2 |
No | OAuth2/OIDC authentication |
user-details |
No | Async UserDetailsService trait |
rate-limit |
No | Rate limiting middleware |
audit |
No | Security event logging |
account-lock |
No | Account locking after failed attempts |
ldap |
No | LDAP/Active Directory authentication |
saml |
No | SAML 2.0 Single Sign-On |
full |
No | All features enabled |
Crate Structure
| Crate | Description |
|---|---|
actix-security |
Unified crate (recommended) - includes core + macros |
actix-security-core |
Core library with middleware, auth, and authorization |
actix-security-codegen |
Procedural macros for declarative security |
Compatibility
| Actix Security | Actix Web | Rust |
|---|---|---|
| 0.2.x | 4.x | 1.70+ |
Spring Security Comparison
Coming from Spring Security? See our Migration Guide and Comparison Table.
Feature Parity
| Spring Security | Actix Security | Status |
|---|---|---|
@Secured |
#[secured] |
Complete |
@PreAuthorize |
#[pre_authorize] |
Complete |
@PermitAll / @DenyAll |
#[permit_all] / #[deny_all] |
Complete |
| HTTP Basic | http-basic feature |
Complete |
| Form Login | form-login feature |
Complete |
| Session Management | session feature |
Complete |
| Remember-Me | remember-me feature |
Complete |
| CSRF Protection | csrf feature |
Complete |
| JWT (OAuth2 Resource Server) | jwt feature |
Complete |
| OAuth2 Login | oauth2 feature |
Complete |
| LDAP Authentication | ldap feature |
Complete |
| SAML 2.0 | saml feature |
Complete |
| Password Encoding | argon2, bcrypt features |
Complete |
| Security Headers | SecurityHeaders middleware |
Complete |
| Method Security | Expression macros | Complete |
| URL-Based Security | RequestMatcherAuthorizer |
Complete |
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is dual-licensed under the MIT License and Apache License 2.0. See LICENSE-MIT and LICENSE-APACHE for details.