From 273e4c74db1b9cf118fbc97d874ccbbe6ba19f35 Mon Sep 17 00:00:00 2001 From: noear Date: Tue, 25 Feb 2025 17:06:41 +0800 Subject: [PATCH] =?UTF-8?q?sa-token-solon-plugin=20=E4=BC=98=E5=8C=96=20sa?= =?UTF-8?q?-oauth2=20=E9=80=82=E9=85=8D=EF=BC=8C=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E4=B8=8E=20spirngboot=20=E7=B1=BB=E4=BC=BC=E7=9A=84=E9=80=82?= =?UTF-8?q?=E9=85=8D=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/dev33/satoken/solon/SaSolonPlugin.java | 6 +- .../solon/oauth2/SaOAuth2AutoConfigure.java | 52 ------ .../solon/oauth2/SaOAuth2BeanInject.java | 162 ++++++++++++++++++ .../solon/oauth2/SaOAuth2BeanRegister.java | 71 ++++++++ 4 files changed, 237 insertions(+), 54 deletions(-) delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2AutoConfigure.java create mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanInject.java create mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanRegister.java diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaSolonPlugin.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaSolonPlugin.java index 7c990516..ea8e9bbb 100644 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaSolonPlugin.java +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaSolonPlugin.java @@ -18,7 +18,8 @@ package cn.dev33.satoken.solon; import cn.dev33.satoken.SaManager; import cn.dev33.satoken.solon.json.SaJsonTemplateForSnack3; import cn.dev33.satoken.solon.model.SaContextForSolon; -import cn.dev33.satoken.solon.oauth2.SaOAuth2AutoConfigure; +import cn.dev33.satoken.solon.oauth2.SaOAuth2BeanInject; +import cn.dev33.satoken.solon.oauth2.SaOAuth2BeanRegister; import cn.dev33.satoken.solon.sso.SaSsoBeanInject; import cn.dev33.satoken.solon.sso.SaSsoBeanRegister; import org.noear.solon.core.AppContext; @@ -47,6 +48,7 @@ public class SaSolonPlugin implements Plugin { context.beanMake(SaSsoBeanInject.class); //sa-oauth2 - context.beanMake(SaOAuth2AutoConfigure.class); + context.beanMake(SaOAuth2BeanRegister.class); + context.beanMake(SaOAuth2BeanInject.class); } } \ No newline at end of file diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2AutoConfigure.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2AutoConfigure.java deleted file mode 100644 index 0bb9b95d..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2AutoConfigure.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.oauth2; - -import cn.dev33.satoken.oauth2.SaOAuth2Manager; -import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig; -import cn.dev33.satoken.oauth2.template.SaOAuth2Template; -import org.noear.solon.annotation.Bean; -import org.noear.solon.annotation.Condition; -import org.noear.solon.annotation.Configuration; -import org.noear.solon.annotation.Inject; -import org.noear.solon.core.AppContext; - -/** - * @author noear - * @since 2.0 - */ - -@Condition(onClass = SaOAuth2Manager.class) -@Configuration -public class SaOAuth2AutoConfigure { - @Condition(onBean = SaOAuth2Template.class) - @Bean - public void template(SaOAuth2Template bean) throws Throwable { - SaOAuth2Manager.setTemplate(bean); - } - - /** - * 获取 OAuth2配置Bean - */ - @Bean - public SaOAuth2ServerConfig getConfig(@Inject(value = "${sa-token.oauth2-server}", required = false) SaOAuth2ServerConfig serverConfig) { - if (serverConfig != null) { - SaOAuth2Manager.setServerConfig(serverConfig); - } - - return serverConfig; - } -} \ No newline at end of file diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanInject.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanInject.java new file mode 100644 index 00000000..e4c56f52 --- /dev/null +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanInject.java @@ -0,0 +1,162 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.dev33.satoken.solon.oauth2; + +import cn.dev33.satoken.oauth2.SaOAuth2Manager; +import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig; +import cn.dev33.satoken.oauth2.dao.SaOAuth2Dao; +import cn.dev33.satoken.oauth2.data.convert.SaOAuth2DataConverter; +import cn.dev33.satoken.oauth2.data.generate.SaOAuth2DataGenerate; +import cn.dev33.satoken.oauth2.data.loader.SaOAuth2DataLoader; +import cn.dev33.satoken.oauth2.data.resolver.SaOAuth2DataResolver; +import cn.dev33.satoken.oauth2.granttype.handler.SaOAuth2GrantTypeHandlerInterface; +import cn.dev33.satoken.oauth2.processor.SaOAuth2ServerProcessor; +import cn.dev33.satoken.oauth2.scope.handler.SaOAuth2ScopeHandlerInterface; +import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy; +import cn.dev33.satoken.oauth2.template.SaOAuth2Template; +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Condition; +import org.noear.solon.annotation.Configuration; + +import java.util.List; + + +// 小提示:如果你在 idea 中运行源码时出现异常:java: 程序包cn.dev33.satoken.oauth2不存在。 +// 在项目根目录进入 cmd,执行 mvn package 即可解决 + + +/** + * 注入 Sa-Token-OAuth2 所需要的组件 + * + * @author click33 + * @since 1.34.0 + */ +@Condition(onClass=SaOAuth2Manager.class) +@Configuration +public class SaOAuth2BeanInject { + + /** + * 注入 OAuth2 配置对象 + * + * @param saOAuth2Config 配置对象 + */ + @Condition(onBean = SaOAuth2ServerConfig.class) + @Bean + public void setSaOAuth2Config(SaOAuth2ServerConfig saOAuth2Config) { + SaOAuth2Manager.setServerConfig(saOAuth2Config); + } + + /** + * 注入 OAuth2 模板代码类 + * + * @param saOAuth2Template 模板代码类 + */ + @Condition(onBean = SaOAuth2Template.class) + @Bean + public void setSaOAuth2Template(SaOAuth2Template saOAuth2Template) { + SaOAuth2Manager.setTemplate(saOAuth2Template); + } + + /** + * 注入 OAuth2 请求处理器 + * + * @param serverProcessor 请求处理器 + */ + @Condition(onBean = SaOAuth2ServerProcessor.class) + @Bean + public void setSaOAuth2Template(SaOAuth2ServerProcessor serverProcessor) { + SaOAuth2ServerProcessor.instance = serverProcessor; + } + + /** + * 注入 OAuth2 数据加载器 + * + * @param dataLoader / + */ + @Condition(onBean = SaOAuth2DataLoader.class) + @Bean + public void setSaOAuth2DataLoader(SaOAuth2DataLoader dataLoader) { + SaOAuth2Manager.setDataLoader(dataLoader); + } + + /** + * 注入 OAuth2 数据解析器 Bean + * + * @param dataResolver / + */ + @Condition(onBean = SaOAuth2DataResolver.class) + @Bean + public void setSaOAuth2DataResolver(SaOAuth2DataResolver dataResolver) { + SaOAuth2Manager.setDataResolver(dataResolver); + } + + /** + * 注入 OAuth2 数据格式转换器 Bean + * + * @param dataConverter / + */ + @Condition(onBean = SaOAuth2DataConverter.class) + @Bean + public void setSaOAuth2DataConverter(SaOAuth2DataConverter dataConverter) { + SaOAuth2Manager.setDataConverter(dataConverter); + } + + /** + * 注入 OAuth2 数据构建器 Bean + * + * @param dataGenerate / + */ + @Condition(onBean = SaOAuth2DataGenerate.class) + @Bean + public void setSaOAuth2DataGenerate(SaOAuth2DataGenerate dataGenerate) { + SaOAuth2Manager.setDataGenerate(dataGenerate); + } + + /** + * 注入 OAuth2 数据持久 Bean + * + * @param dao / + */ + @Condition(onBean = SaOAuth2Dao.class) + @Bean + public void setSaOAuth2Dao(SaOAuth2Dao dao) { + SaOAuth2Manager.setDao(dao); + } + + /** + * 注入自定义 scope 处理器 + * + * @param handlerList 自定义 scope 处理器集合 + */ + @Bean + public void setSaOAuth2ScopeHandler(List handlerList) { + for (SaOAuth2ScopeHandlerInterface handler : handlerList) { + SaOAuth2Strategy.instance.registerScopeHandler(handler); + } + } + + /** + * 注入自定义 grant_type 处理器 + * + * @param handlerList 自定义 grant_type 处理器集合 + */ + @Bean + public void setSaOAuth2GrantTypeHandlerInterface(List handlerList) { + for (SaOAuth2GrantTypeHandlerInterface handler : handlerList) { + SaOAuth2Strategy.instance.registerGrantTypeHandler(handler); + } + } +} diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanRegister.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanRegister.java new file mode 100644 index 00000000..8f95403f --- /dev/null +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/oauth2/SaOAuth2BeanRegister.java @@ -0,0 +1,71 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.dev33.satoken.solon.oauth2; + +import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface; +import cn.dev33.satoken.oauth2.SaOAuth2Manager; +import cn.dev33.satoken.oauth2.annotation.SaCheckAccessToken; +import cn.dev33.satoken.oauth2.annotation.SaCheckClientIdSecret; +import cn.dev33.satoken.oauth2.annotation.SaCheckClientToken; +import cn.dev33.satoken.oauth2.annotation.handler.SaCheckAccessTokenHandler; +import cn.dev33.satoken.oauth2.annotation.handler.SaCheckClientIdSecretHandler; +import cn.dev33.satoken.oauth2.annotation.handler.SaCheckClientTokenHandler; +import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig; +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Condition; +import org.noear.solon.annotation.Configuration; +import org.noear.solon.annotation.Inject; + +/** + * 注册 Sa-Token-OAuth2 所需要的Bean + * + * @author click33 + * @since 1.34.0 + */ +@Condition(onClass=SaOAuth2Manager.class) +@Configuration +public class SaOAuth2BeanRegister { + + /** + * 获取 OAuth2 配置 Bean + * + * @return 配置对象 + */ + @Bean + public SaOAuth2ServerConfig getSaOAuth2Config(@Inject(value = "${sa-token.oauth2-server}", required = false) SaOAuth2ServerConfig serverConfig) { + if (serverConfig == null) { + return new SaOAuth2ServerConfig(); + } else { + return serverConfig; + } + } + + // 自定义注解处理器 + @Bean + public SaAnnotationHandlerInterface getSaCheckAccessTokenHandler() { + return new SaCheckAccessTokenHandler(); + } + + @Bean + public SaAnnotationHandlerInterface getSaCheckClientTokenHandler() { + return new SaCheckClientTokenHandler(); + } + + @Bean + public SaAnnotationHandlerInterface getSaCheckClientIdSecretHandler() { + return new SaCheckClientIdSecretHandler(); + } +}