From 82f7d7f78c22bc932e87fc59d4efe098913d4282 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Sun, 4 Jul 2021 22:31:28 +0800 Subject: [PATCH] =?UTF-8?q?starter=E5=8C=85=E6=9E=B6=E6=9E=84=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/pj/SaTokenWebfluxDemoApplication.java | 2 +- .../java/com/pj/satoken/SaTokenConfigure.java | 2 +- sa-token-doc/doc/use/config.md | 13 +++++ .../satoken/reactor/spring/SaBeanInject.java} | 33 ++----------- .../reactor/spring/SaBeanRegister.java | 47 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 +- .../dev33/satoken/spring/SaBeanInject.java} | 45 +++--------------- .../dev33/satoken/spring/SaBeanRegister.java | 38 +++++++++++++++ .../main/resources/META-INF/spring.factories | 2 +- 9 files changed, 113 insertions(+), 71 deletions(-) rename sa-token-starter/{sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaTokenSpringAutowired.java => sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanInject.java} (79%) create mode 100644 sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanRegister.java rename sa-token-starter/{sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenSpringAutowired.java => sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java} (71%) create mode 100644 sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanRegister.java diff --git a/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/SaTokenWebfluxDemoApplication.java b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/SaTokenWebfluxDemoApplication.java index c3914628..783c0e8e 100644 --- a/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/SaTokenWebfluxDemoApplication.java +++ b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/SaTokenWebfluxDemoApplication.java @@ -17,7 +17,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import cn.dev33.satoken.SaManager; /** - * sa-token整合webflux 示例 + * Sa-Token整合webflux 示例 * @author kong * */ diff --git a/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/SaTokenConfigure.java b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/SaTokenConfigure.java index 0a8089f6..7dfe7b7c 100644 --- a/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/SaTokenConfigure.java +++ b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/SaTokenConfigure.java @@ -14,7 +14,7 @@ import cn.dev33.satoken.reactor.filter.SaReactorFilter; */ @Configuration public class SaTokenConfigure { - + /** * 注册 [sa-token全局过滤器] */ diff --git a/sa-token-doc/doc/use/config.md b/sa-token-doc/doc/use/config.md index 76b4b127..9dd9fc7b 100644 --- a/sa-token-doc/doc/use/config.md +++ b/sa-token-doc/doc/use/config.md @@ -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中的配置合并`** + --- ### 所有可配置项 diff --git a/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaTokenSpringAutowired.java b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanInject.java similarity index 79% rename from sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaTokenSpringAutowired.java rename to sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanInject.java index b1eb9947..d456bf5c 100644 --- a/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaTokenSpringAutowired.java +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanInject.java @@ -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); } diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanRegister.java b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanRegister.java new file mode 100644 index 00000000..7cdff979 --- /dev/null +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaBeanRegister.java @@ -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 + *

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); + } + }; + } + +} diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/resources/META-INF/spring.factories b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/resources/META-INF/spring.factories index cc23631c..399afb5a 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1 +1 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.reactor.spring.SaTokenSpringAutowired \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.reactor.spring.SaBeanInject \ No newline at end of file diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenSpringAutowired.java b/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java similarity index 71% rename from sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenSpringAutowired.java rename to sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java index 062dff85..a59b01de 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenSpringAutowired.java +++ b/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java @@ -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); } - } diff --git a/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanRegister.java b/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanRegister.java new file mode 100644 index 00000000..db4805cc --- /dev/null +++ b/sa-token-starter/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SaBeanRegister.java @@ -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 + *

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(); + } + +} diff --git a/sa-token-starter/sa-token-spring-boot-starter/src/main/resources/META-INF/spring.factories b/sa-token-starter/sa-token-spring-boot-starter/src/main/resources/META-INF/spring.factories index 1d3802f2..bc9a42c0 100644 --- a/sa-token-starter/sa-token-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/sa-token-starter/sa-token-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1 +1 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.spring.SaTokenSpringAutowired \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.spring.SaBeanInject \ No newline at end of file