sa_token_plugin_axum/
lib.rs1pub mod layer;
47pub mod extractor;
48pub mod middleware;
49pub mod adapter;
50
51pub use layer::SaTokenLayer;
55pub use extractor::{SaTokenExtractor, OptionalSaTokenExtractor, LoginIdExtractor};
56pub use middleware::{SaTokenMiddleware, SaCheckLoginLayer, SaCheckLoginMiddleware, SaCheckPermissionLayer, SaCheckPermissionMiddleware};
57
58pub use sa_token_core::{self, prelude::*};
59pub use sa_token_adapter::{self, storage::SaStorage, framework::FrameworkAdapter};
60pub use sa_token_macro::*;
61
62#[cfg(feature = "memory")]
68pub use sa_token_storage_memory::MemoryStorage;
69
70#[cfg(feature = "redis")]
72pub use sa_token_storage_redis::RedisStorage;
73
74#[cfg(feature = "database")]
76pub use sa_token_storage_database::DatabaseStorage;
77
78use std::sync::Arc;
79
80#[derive(Clone)]
82pub struct SaTokenState {
83 pub manager: Arc<SaTokenManager>,
84}
85
86impl SaTokenState {
87 pub fn new(storage: Arc<dyn SaStorage>, config: SaTokenConfig) -> Self {
89 Self {
90 manager: Arc::new(SaTokenManager::new(storage, config)),
91 }
92 }
93
94 pub fn from_manager(manager: SaTokenManager) -> Self {
96 Self {
97 manager: Arc::new(manager),
98 }
99 }
100
101 pub fn builder() -> SaTokenStateBuilder {
116 SaTokenStateBuilder::default()
117 }
118}
119
120#[derive(Default)]
122pub struct SaTokenStateBuilder {
123 config_builder: sa_token_core::config::SaTokenConfigBuilder,
124}
125
126impl SaTokenStateBuilder {
127 pub fn token_name(mut self, name: impl Into<String>) -> Self {
128 self.config_builder = self.config_builder.token_name(name);
129 self
130 }
131
132 pub fn timeout(mut self, timeout: i64) -> Self {
133 self.config_builder = self.config_builder.timeout(timeout);
134 self
135 }
136
137 pub fn active_timeout(mut self, timeout: i64) -> Self {
138 self.config_builder = self.config_builder.active_timeout(timeout);
139 self
140 }
141
142 pub fn auto_renew(mut self, enabled: bool) -> Self {
144 self.config_builder = self.config_builder.auto_renew(enabled);
145 self
146 }
147
148 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
149 self.config_builder = self.config_builder.is_concurrent(concurrent);
150 self
151 }
152
153 pub fn is_share(mut self, share: bool) -> Self {
154 self.config_builder = self.config_builder.is_share(share);
155 self
156 }
157
158 pub fn token_style(mut self, style: sa_token_core::config::TokenStyle) -> Self {
159 self.config_builder = self.config_builder.token_style(style);
160 self
161 }
162
163 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
164 self.config_builder = self.config_builder.token_prefix(prefix);
165 self
166 }
167
168 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
169 self.config_builder = self.config_builder.jwt_secret_key(key);
170 self
171 }
172
173 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
174 self.config_builder = self.config_builder.storage(storage);
175 self
176 }
177
178 pub fn build(self) -> SaTokenState {
179 let manager = self.config_builder.build();
182 SaTokenState {
185 manager: Arc::new(manager),
186 }
187 }
188}