sa_token_plugin_actix_web/
lib.rs1pub mod middleware;
8pub mod extractor;
9pub mod adapter;
10
11pub use middleware::{SaTokenMiddleware, SaCheckLoginMiddleware};
12pub use extractor::{SaTokenExtractor, OptionalSaTokenExtractor, LoginIdExtractor};
13pub use adapter::{ActixRequestAdapter, ActixResponseAdapter};
14
15use std::sync::Arc;
16use actix_web::web::Data;
17use sa_token_core::SaTokenManager;
18use sa_token_adapter::storage::SaStorage;
19
20pub type SaTokenData = Data<SaTokenState>;
22
23#[derive(Clone)]
25pub struct SaTokenState {
26 pub manager: Arc<SaTokenManager>,
27}
28
29impl SaTokenState {
30 pub fn builder() -> SaTokenStateBuilder {
32 SaTokenStateBuilder::new()
33 }
34}
35
36#[derive(Default)]
38pub struct SaTokenStateBuilder {
39 config_builder: sa_token_core::config::SaTokenConfigBuilder,
40}
41
42impl SaTokenStateBuilder {
43 pub fn new() -> Self {
44 Self::default()
45 }
46
47 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
48 self.config_builder = self.config_builder.storage(storage);
49 self
50 }
51
52 pub fn token_name(mut self, name: impl Into<String>) -> Self {
53 self.config_builder = self.config_builder.token_name(name);
54 self
55 }
56
57 pub fn timeout(mut self, timeout: i64) -> Self {
58 self.config_builder = self.config_builder.timeout(timeout);
59 self
60 }
61
62 pub fn active_timeout(mut self, timeout: i64) -> Self {
63 self.config_builder = self.config_builder.active_timeout(timeout);
64 self
65 }
66
67 pub fn auto_renew(mut self, enabled: bool) -> Self {
69 self.config_builder = self.config_builder.auto_renew(enabled);
70 self
71 }
72
73 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
74 self.config_builder = self.config_builder.is_concurrent(concurrent);
75 self
76 }
77
78 pub fn is_share(mut self, share: bool) -> Self {
79 self.config_builder = self.config_builder.is_share(share);
80 self
81 }
82
83 pub fn token_style(mut self, style: sa_token_core::config::TokenStyle) -> Self {
85 self.config_builder = self.config_builder.token_style(style);
86 self
87 }
88
89 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
90 self.config_builder = self.config_builder.token_prefix(prefix);
91 self
92 }
93
94 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
95 self.config_builder = self.config_builder.jwt_secret_key(key);
96 self
97 }
98
99 pub fn build(self) -> Data<SaTokenState> {
100 let manager = self.config_builder.build();
101
102 sa_token_core::StpUtil::init_manager(manager.clone());
104
105 Data::new(SaTokenState {
106 manager: Arc::new(manager),
107 })
108 }
109}
110