starter包架构调整

This commit is contained in:
click33 2021-07-04 22:31:28 +08:00
parent 12d76e34bb
commit 82f7d7f78c
9 changed files with 113 additions and 71 deletions

View File

@ -17,7 +17,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import cn.dev33.satoken.SaManager;
/**
* sa-token整合webflux 示例
* Sa-Token整合webflux 示例
* @author kong
*
*/

View File

@ -14,7 +14,7 @@ import cn.dev33.satoken.reactor.filter.SaReactorFilter;
*/
@Configuration
public class SaTokenConfigure {
/**
* 注册 [sa-token全局过滤器]
*/

View File

@ -33,6 +33,7 @@ sa-token:
### 方式2、通过代码配置
方式1
``` java
/**
* Sa-Token代码方式进行配置
@ -58,6 +59,18 @@ public class SaTokenConfigure {
}
```
方式2
``` java
// 以代码的方式配置Sa-Token-Config
@Autowired
public void configSaToken(SaTokenConfig config) {
// config.setTokenName("satoken333"); // token名称 (同时也是cookie名称)
// ...
}
```
PS两者的区别在于**`方式1会覆盖yml中的配置方式2会与yml中的配置合并`**
---
### 所有可配置项

View File

@ -1,9 +1,7 @@
package cn.dev33.satoken.spring;
package cn.dev33.satoken.reactor.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.util.PathMatcher;
@ -24,26 +22,15 @@ import cn.dev33.satoken.temp.SaTempInterface;
*
*/
@Component
@Import(SaHistoryVersionInject.class)
public class SaTokenSpringAutowired {
@Import({SaHistoryVersionInject.class, SaBeanRegister.class})
public class SaBeanInject {
/**
* 获取配置Bean
*
* @return 配置对象
*/
@Bean
@ConfigurationProperties(prefix = "sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
/**
* 注入配置Bean
*
* @param saTokenConfig 配置对象
*/
@Autowired
@Autowired(required = false)
public void setConfig(SaTokenConfig saTokenConfig) {
SaManager.setConfig(saTokenConfig);
}
@ -78,22 +65,12 @@ public class SaTokenSpringAutowired {
SaManager.setSaTokenAction(saTokenAction);
}
/**
* 获取容器交互Bean (Spring版)
*
* @return 容器交互Bean (Spring版)
*/
@Bean
public SaTokenContext getSaTokenContext() {
return new SaTokenContextForSpring();
}
/**
* 注入容器交互Bean
*
* @param saTokenContext SaTokenContext对象
*/
@Autowired
@Autowired(required = false)
public void setSaTokenContext(SaTokenContext saTokenContext) {
SaManager.setSaTokenContext(saTokenContext);
}

View File

@ -0,0 +1,47 @@
package cn.dev33.satoken.reactor.spring;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.context.SaTokenContextForThreadLocal;
/**
* 注册Sa-Token所需要的Bean
* <p> Bean 的注册与注入应该分开在两个文件中否则在某些场景下会造成循环依赖
* @author kong
*
*/
public class SaBeanRegister {
/**
* 获取配置Bean
*
* @return 配置对象
*/
@Bean
@ConfigurationProperties(prefix = "sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
/**
* 获取容器交互Bean (ThreadLocal版)
*
* @return 容器交互Bean (ThreadLocal版)
*/
@Bean
public SaTokenContext getSaTokenContext() {
return new SaTokenContextForThreadLocal() {
/**
* 重写路由匹配方法
*/
@Override
public boolean matchPath(String pattern, String path) {
return SaPathMatcherHolder.getPathMatcher().match(pattern, path);
}
};
}
}

View File

@ -1 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.reactor.spring.SaTokenSpringAutowired
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.reactor.spring.SaBeanInject

View File

@ -1,9 +1,7 @@
package cn.dev33.satoken.reactor.spring;
package cn.dev33.satoken.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.util.PathMatcher;
@ -12,39 +10,27 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.context.SaTokenContextForThreadLocal;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.temp.SaTempInterface;
/**
* 利用spring的自动装配来加载开发者重写的Bean
* 注入Sa-Token所需要的Bean
*
* @author kong
*
*/
@Component
@Import(SaHistoryVersionInject.class)
public class SaTokenSpringAutowired {
/**
* 获取配置Bean
*
* @return 配置对象
*/
@Bean
@ConfigurationProperties(prefix = "sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
@Import({SaBeanRegister.class, SaHistoryVersionInject.class})
public class SaBeanInject {
/**
* 注入配置Bean
*
* @param saTokenConfig 配置对象
*/
@Autowired
@Autowired(required = false)
public void setConfig(SaTokenConfig saTokenConfig) {
SaManager.setConfig(saTokenConfig);
}
@ -79,30 +65,12 @@ public class SaTokenSpringAutowired {
SaManager.setSaTokenAction(saTokenAction);
}
/**
* 获取容器交互Bean (ThreadLocal版)
*
* @return 容器交互Bean (ThreadLocal版)
*/
@Bean
public SaTokenContext getSaTokenContext() {
return new SaTokenContextForThreadLocal() {
/**
* 重写路由匹配方法
*/
@Override
public boolean matchPath(String pattern, String path) {
return SaPathMatcherHolder.getPathMatcher().match(pattern, path);
}
};
}
/**
* 注入容器交互Bean
*
* @param saTokenContext SaTokenContext对象
*/
@Autowired
@Autowired(required = false)
public void setSaTokenContext(SaTokenContext saTokenContext) {
SaManager.setSaTokenContext(saTokenContext);
}
@ -138,5 +106,4 @@ public class SaTokenSpringAutowired {
SaPathMatcherHolder.setPathMatcher(pathMatcher);
}
}

View File

@ -0,0 +1,38 @@
package cn.dev33.satoken.spring;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaTokenContext;
/**
* 注册Sa-Token所需要的Bean
* <p> Bean 的注册与注入应该分开在两个文件中否则在某些场景下会造成循环依赖
* @author kong
*
*/
public class SaBeanRegister {
/**
* 获取配置Bean
*
* @return 配置对象
*/
@Bean
@ConfigurationProperties(prefix = "sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
/**
* 获取容器交互Bean (Spring版)
*
* @return 容器交互Bean (Spring版)
*/
@Bean
public SaTokenContext getSaTokenContext() {
return new SaTokenContextForSpring();
}
}

View File

@ -1 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.spring.SaTokenSpringAutowired
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.spring.SaBeanInject