sa_token_plugin_actix_web/
lib.rs

1// Author: 金书记
2//
3//! # sa-token-plugin-actix-web
4//! 
5//! Actix-web框架集成插件 - 一站式认证授权解决方案
6//! 
7//! ## 快速开始
8//! 
9//! 只需要导入这一个包,即可使用所有功能:
10//! 
11//! ```toml
12//! [dependencies]
13//! sa-token-plugin-actix-web = "0.1.3"  # 默认使用内存存储
14//! # 或者使用 Redis 存储
15//! sa-token-plugin-actix-web = { version = "0.1.3", features = ["redis"] }
16//! ```
17//! 
18//! ## 使用示例
19//! 
20//! ```rust,ignore
21//! use sa_token_plugin_actix_web::*;
22//! 
23//! // 1. 初始化(使用内存存储)
24//! let state = SaTokenState::builder()
25//!     .storage(Arc::new(MemoryStorage::new()))  // 已重新导出
26//!     .timeout(7200)
27//!     .build();
28//! 
29//! // 2. 登录
30//! let token = state.manager().login("user123").await?;
31//! 
32//! // 3. 使用宏检查权限
33//! #[sa_check_login]
34//! async fn user_info() -> impl Responder {
35//!     "User info"
36//! }
37//! 
38//! #[sa_check_permission("admin")]
39//! async fn admin_panel() -> impl Responder {
40//!     "Admin panel"
41//! }
42//! ```
43
44pub mod middleware;
45pub mod extractor;
46pub mod adapter;
47pub mod layer;
48
49pub use middleware::{SaCheckLoginMiddleware, SaTokenMiddleware};
50pub use layer::SaTokenLayer;
51pub use extractor::{SaTokenExtractor, OptionalSaTokenExtractor, LoginIdExtractor};
52pub use adapter::{ActixRequestAdapter, ActixResponseAdapter};
53
54pub use sa_token_core::{self, prelude::*};
55pub use sa_token_adapter::{self, storage::SaStorage, framework::FrameworkAdapter};
56pub use sa_token_macro::*;
57
58// ============================================================================
59// 重新导出存储实现(根据 feature 条件编译)
60// ============================================================================
61
62/// 内存存储(默认启用)
63#[cfg(feature = "memory")]
64pub use sa_token_storage_memory::MemoryStorage;
65
66/// Redis 存储
67#[cfg(feature = "redis")]
68pub use sa_token_storage_redis::RedisStorage;
69
70/// 数据库存储
71#[cfg(feature = "database")]
72pub use sa_token_storage_database::DatabaseStorage;
73
74use std::sync::Arc;
75use actix_web::web::Data;
76
77/// Actix-web应用数据
78pub type SaTokenData = Data<SaTokenState>;
79
80/// 应用状态
81#[derive(Clone)]
82pub struct SaTokenState {
83    pub manager: Arc<SaTokenManager>,
84}
85
86impl SaTokenState {
87    /// 创建状态构建器
88    pub fn builder() -> SaTokenStateBuilder {
89        SaTokenStateBuilder::new()
90    }
91}
92
93/// 状态构建器
94#[derive(Default)]
95pub struct SaTokenStateBuilder {
96    config_builder: sa_token_core::config::SaTokenConfigBuilder,
97}
98
99impl SaTokenStateBuilder {
100    pub fn new() -> Self {
101        Self::default()
102    }
103    
104    pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
105        self.config_builder = self.config_builder.storage(storage);
106        self
107    }
108    
109    pub fn token_name(mut self, name: impl Into<String>) -> Self {
110        self.config_builder = self.config_builder.token_name(name);
111        self
112    }
113    
114    pub fn timeout(mut self, timeout: i64) -> Self {
115        self.config_builder = self.config_builder.timeout(timeout);
116        self
117    }
118    
119    pub fn active_timeout(mut self, timeout: i64) -> Self {
120        self.config_builder = self.config_builder.active_timeout(timeout);
121        self
122    }
123    
124    /// 设置是否开启自动续签
125    pub fn auto_renew(mut self, enabled: bool) -> Self {
126        self.config_builder = self.config_builder.auto_renew(enabled);
127        self
128    }
129    
130    pub fn is_concurrent(mut self, concurrent: bool) -> Self {
131        self.config_builder = self.config_builder.is_concurrent(concurrent);
132        self
133    }
134    
135    pub fn is_share(mut self, share: bool) -> Self {
136        self.config_builder = self.config_builder.is_share(share);
137        self
138    }
139    
140    /// 设置 Token 风格
141    pub fn token_style(mut self, style: sa_token_core::config::TokenStyle) -> Self {
142        self.config_builder = self.config_builder.token_style(style);
143        self
144    }
145    
146    pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
147        self.config_builder = self.config_builder.token_prefix(prefix);
148        self
149    }
150    
151    pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
152        self.config_builder = self.config_builder.jwt_secret_key(key);
153        self
154    }
155    
156    pub fn build(self) -> Data<SaTokenState> {
157        let manager = self.config_builder.build();
158                
159        Data::new(SaTokenState {
160            manager: Arc::new(manager),
161        })
162    }
163}
164