sa_token_plugin_actix_web/
lib.rs1pub 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#[cfg(feature = "memory")]
64pub use sa_token_storage_memory::MemoryStorage;
65
66#[cfg(feature = "redis")]
68pub use sa_token_storage_redis::RedisStorage;
69
70#[cfg(feature = "database")]
72pub use sa_token_storage_database::DatabaseStorage;
73
74use std::sync::Arc;
75use actix_web::web::Data;
76
77pub type SaTokenData = Data<SaTokenState>;
79
80#[derive(Clone)]
82pub struct SaTokenState {
83 pub manager: Arc<SaTokenManager>,
84}
85
86impl SaTokenState {
87 pub fn builder() -> SaTokenStateBuilder {
89 SaTokenStateBuilder::new()
90 }
91}
92
93#[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 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 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