!324 sa-token-solon-plugin 优化 sa-token, sa-sso, sa-oauth2 适配,保持与 spirngboot 类似的适配体验

Merge pull request !324 from 西东/dev
This commit is contained in:
刘潇
2025-02-25 09:11:37 +00:00
committed by Gitee
11 changed files with 512 additions and 323 deletions

View File

@@ -43,7 +43,7 @@ import org.noear.solon.annotation.*;
import java.util.List;
/**
* 注入 Sa-Token 所需要的 Bean //todo: 再去插件添加context.beanMake(SaBeanInject.class);
* 注入 Sa-Token 所需要的 Bean
*
* @author click33
* @since 1.34.0
@@ -52,22 +52,22 @@ import java.util.List;
public class SaBeanInject {
/**
* 组件注入
* <p> 为确保 Log 组件正常打印,必须将 SaLog 和 SaTokenConfig 率先初始化 </p>
*
* @param log log 对象
* 组件注入
* <p> 为确保 Log 组件正常打印,必须将 SaLog 和 SaTokenConfig 率先初始化 </p>
*
* @param log log 对象
* @param saTokenConfig 配置对象
*/
public SaBeanInject(
@Inject(required = false) SaLog log,
@Inject(required = false) SaTokenConfig saTokenConfig,
@Inject(required = false) SaTokenPluginHolder pluginHolder
){
if(log != null) {
) {
if (log != null) {
SaManager.setLog(log);
}
if(saTokenConfig != null) {
if (saTokenConfig != null) {
SaManager.setConfig(saTokenConfig);
}
@@ -78,11 +78,11 @@ public class SaBeanInject {
pluginHolder.init();
SaTokenPluginHolder.instance = pluginHolder;
}
/**
* 注入持久化Bean
*
* @param saTokenDao SaTokenDao对象
*
* @param saTokenDao SaTokenDao对象
*/
@Condition(onBean = SaTokenDao.class)
@Bean
@@ -92,8 +92,8 @@ public class SaBeanInject {
/**
* 注入权限认证Bean
*
* @param stpInterface StpInterface对象
*
* @param stpInterface StpInterface对象
*/
@Condition(onBean = StpInterface.class)
@Bean
@@ -103,8 +103,8 @@ public class SaBeanInject {
/**
* 注入上下文Bean
*
* @param saTokenContext SaTokenContext对象
*
* @param saTokenContext SaTokenContext对象
*/
@Condition(onBean = SaTokenContext.class)
@Bean
@@ -114,8 +114,8 @@ public class SaBeanInject {
/**
* 注入二级上下文Bean
*
* @param saTokenSecondContextCreator 二级上下文创建器
*
* @param saTokenSecondContextCreator 二级上下文创建器
*/
@Condition(onBean = SaTokenSecondContextCreator.class)
@Bean
@@ -125,8 +125,8 @@ public class SaBeanInject {
/**
* 注入侦听器Bean
*
* @param listenerList 侦听器集合
*
* @param listenerList 侦听器集合
*/
@Bean
public void setSaTokenListener(List<SaTokenListener> listenerList) {
@@ -147,8 +147,8 @@ public class SaBeanInject {
/**
* 注入临时令牌验证模块 Bean
*
* @param saTemp saTemp对象
*
* @param saTemp saTemp对象
*/
@Condition(onBean = SaTempInterface.class)
@Bean
@@ -158,8 +158,8 @@ public class SaBeanInject {
/**
* 注入 Same-Token 模块 Bean
*
* @param saSameTemplate saSameTemplate对象
*
* @param saSameTemplate saSameTemplate对象
*/
@Condition(onBean = SaSameTemplate.class)
@Bean
@@ -168,9 +168,9 @@ public class SaBeanInject {
}
/**
* 注入 Sa-Token Http Basic 认证模块
*
* @param saBasicTemplate saBasicTemplate对象
* 注入 Sa-Token Http Basic 认证模块
*
* @param saBasicTemplate saBasicTemplate对象
*/
@Condition(onBean = SaHttpBasicTemplate.class)
@Bean
@@ -190,9 +190,9 @@ public class SaBeanInject {
}
/**
* 注入自定义的 JSON 转换器 Bean
*
* @param saJsonTemplate JSON 转换器
* 注入自定义的 JSON 转换器 Bean
*
* @param saJsonTemplate JSON 转换器
*/
@Condition(onBean = SaJsonTemplate.class)
@Bean
@@ -212,9 +212,9 @@ public class SaBeanInject {
}
/**
* 注入自定义的 参数签名 Bean
*
* @param saSignTemplate 参数签名 Bean
* 注入自定义的 参数签名 Bean
*
* @param saSignTemplate 参数签名 Bean
*/
@Condition(onBean = SaSignTemplate.class)
@Bean
@@ -223,24 +223,13 @@ public class SaBeanInject {
}
/**
* 注入自定义的 StpLogic
* @param stpLogic /
* 注入自定义的 StpLogic
*
* @param stpLogic /
*/
@Condition(onBean = StpLogic.class)
@Bean
public void setStpLogic(StpLogic stpLogic) {
StpUtil.setStpLogic(stpLogic);
}
/**
* 利用自动注入特性获取Spring框架内部使用的路由匹配器
*
* @param pathMatcher 要设置的 pathMatcher
*/
// @Condition(onBean = StpLogic.class) //SaPathMatcherHolder 这个类可能是新的未刷到(后面补上)
// @Bean
// public void setPathMatcher(PathMatcher pathMatcher) {
// SaPathMatcherHolder.setPathMatcher(pathMatcher);
// }
}

View File

@@ -0,0 +1,44 @@
/*
* 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;
import cn.dev33.satoken.config.SaTokenConfig;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
/**
* 注册Sa-Token所需要的Bean
* <p> Bean 的注册与注入应该分开在两个文件中,否则在某些场景下会造成循环依赖
* @author click33
*
*/
@Configuration
public class SaBeanRegister {
/**
* 获取配置Bean
*
* @return 配置对象
*/
@Bean
public SaTokenConfig getSaTokenConfig(@Inject(value = "${sa-token}", required = false) SaTokenConfig config) {
if (config == null) {
return new SaTokenConfig();
} else {
return config;
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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;
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.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;
import org.noear.solon.core.Plugin;
/**
* @author noear
* @since 1.4
*/
public class SaSolonPlugin implements Plugin {
@Override
public void start(AppContext context) {
// 注入上下文Bean
SaManager.setSaTokenContext(new SaContextForSolon());
// 注入JSON解析器Bean
SaManager.setSaJsonTemplate(new SaJsonTemplateForSnack3());
//sa-token
context.beanMake(SaBeanRegister.class);
context.beanMake(SaBeanInject.class);
//sa-sso
context.beanMake(SaSsoBeanRegister.class);
context.beanMake(SaSsoBeanInject.class);
//sa-oauth2
context.beanMake(SaOAuth2BeanRegister.class);
context.beanMake(SaOAuth2BeanInject.class);
}
}

View File

@@ -1,152 +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;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicTemplate;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
import cn.dev33.satoken.httpauth.digest.SaHttpDigestTemplate;
import cn.dev33.satoken.httpauth.digest.SaHttpDigestUtil;
import cn.dev33.satoken.json.SaJsonTemplate;
import cn.dev33.satoken.listener.SaTokenEventCenter;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.same.SaSameTemplate;
import cn.dev33.satoken.serializer.SaSerializerTemplate;
import cn.dev33.satoken.sign.SaSignTemplate;
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.sso.SaSsoAutoConfigure;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import cn.dev33.satoken.temp.SaTempInterface;
import org.noear.solon.Solon;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
/**
* @author noear
* @since 1.4
*/
public class XPluginImp implements Plugin {
@Override
public void start(AppContext context) {
context.beanMake(SaSsoAutoConfigure.class);
context.beanMake(SaOAuth2AutoConfigure.class);
//context.beanMake(SaBeanInject.class); //暂进未启用
// 注入上下文Bean
SaManager.setSaTokenContext(new SaContextForSolon());
// 注入JSON解析器Bean
SaManager.setSaJsonTemplate(new SaJsonTemplateForSnack3());
//注入配置Bean
SaTokenConfig saTokenConfig = Solon.cfg().getBean("sa-token", SaTokenConfig.class);
if (saTokenConfig != null) {
SaManager.setConfig(saTokenConfig);
}
// Sa-Token 日志输出 Bean
context.getBeanAsync(SaLog.class, bean -> {
SaManager.setLog(bean);
});
//注入 SaTokenConfig
context.getBeanAsync(SaTokenConfig.class, bean -> {
SaManager.setConfig(bean);
});
// 注入Dao Bean
context.getBeanAsync(SaTokenDao.class, bean -> {
SaManager.setSaTokenDao(bean);
});
// 注入二级上下文 Bean
context.getBeanAsync(SaTokenSecondContextCreator.class, bean -> {
SaManager.setSaTokenSecondContext(bean.create());
});
// 注入侦听器 Bean (可以有多个)
context.subBeansOfType(SaTokenListener.class, sl -> {
SaTokenEventCenter.registerListener(sl);
});
// 注入自定义注解处理器 Bean (可以有多个)
context.subBeansOfType(SaAnnotationHandlerInterface.class, sl -> {
SaAnnotationStrategy.instance.registerAnnotationHandler(sl);
});
// 注入权限认证 Bean
context.getBeanAsync(StpInterface.class, bean -> {
SaManager.setStpInterface(bean);
});
// 注入持久化 Bean
context.getBeanAsync(SaTokenDao.class, bean -> {
SaManager.setSaTokenDao(bean);
});
// 临时令牌验证模块 Bean
context.getBeanAsync(SaTempInterface.class, bean -> {
SaManager.setSaTemp(bean);
});
// Sa-Token Same-Token 模块 Bean
context.getBeanAsync(SaSameTemplate.class, bean -> {
SaManager.setSaSameTemplate(bean);
});
// Sa-Token Http Basic 认证模块 Bean
context.getBeanAsync(SaHttpBasicTemplate.class, bean -> {
SaHttpBasicUtil.saHttpBasicTemplate = bean;
});
// Sa-Token Http Digest 认证模块 Bean
context.getBeanAsync(SaHttpDigestTemplate.class, bean -> {
SaHttpDigestUtil.saHttpDigestTemplate = bean;
});
// Sa-Token JSON 转换器 Bean
context.getBeanAsync(SaJsonTemplate.class, bean -> {
SaManager.setSaJsonTemplate(bean);
});
// Sa-Token 序列化器 Bean
context.getBeanAsync(SaSerializerTemplate.class, bean -> {
SaManager.setSaSerializerTemplate(bean);
});
// Sa-Token 参数签名算法 Bean
context.getBeanAsync(SaSignTemplate.class, bean -> {
SaManager.setSaSignTemplate(bean);
});
// 自定义 StpLogic 对象 //容器层面只能有一个要多个得自己在Util上处理
context.getBeanAsync(StpLogic.class, bean -> {
StpUtil.setStpLogic(bean);
});
}
}

View File

@@ -1,53 +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 {
@Bean
public void init(AppContext appContext) throws Throwable {
appContext.subBeansOfType(SaOAuth2Template.class, bean -> {
SaOAuth2Manager.setTemplate(bean);
});
appContext.subBeansOfType(SaOAuth2ServerConfig.class, bean -> {
SaOAuth2Manager.setServerConfig(bean);
});
}
/**
* 获取 OAuth2配置Bean
*/
@Bean
public SaOAuth2ServerConfig getConfig(@Inject(value = "${sa-token.oauth2-server}", required = false) SaOAuth2ServerConfig oAuth2Config) {
return oAuth2Config;
}
}

View File

@@ -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<SaOAuth2ScopeHandlerInterface> handlerList) {
for (SaOAuth2ScopeHandlerInterface handler : handlerList) {
SaOAuth2Strategy.instance.registerScopeHandler(handler);
}
}
/**
* 注入自定义 grant_type 处理器
*
* @param handlerList 自定义 grant_type 处理器集合
*/
@Bean
public void setSaOAuth2GrantTypeHandlerInterface(List<SaOAuth2GrantTypeHandlerInterface> handlerList) {
for (SaOAuth2GrantTypeHandlerInterface handler : handlerList) {
SaOAuth2Strategy.instance.registerGrantTypeHandler(handler);
}
}
}

View File

@@ -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<SaCheckAccessToken> getSaCheckAccessTokenHandler() {
return new SaCheckAccessTokenHandler();
}
@Bean
public SaAnnotationHandlerInterface<SaCheckClientToken> getSaCheckClientTokenHandler() {
return new SaCheckClientTokenHandler();
}
@Bean
public SaAnnotationHandlerInterface<SaCheckClientIdSecret> getSaCheckClientIdSecretHandler() {
return new SaCheckClientIdSecretHandler();
}
}

View File

@@ -1,71 +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.sso;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.config.SaSsoClientConfig;
import cn.dev33.satoken.sso.config.SaSsoServerConfig;
import cn.dev33.satoken.sso.processor.SaSsoClientProcessor;
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
import cn.dev33.satoken.sso.template.SaSsoClientTemplate;
import cn.dev33.satoken.sso.template.SaSsoServerTemplate;
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 = SaSsoManager.class)
@Configuration
public class SaSsoAutoConfigure {
@Bean
public void init(AppContext appContext) throws Throwable {
appContext.subBeansOfType(SaSsoServerTemplate.class, bean -> {
SaSsoServerProcessor.instance.ssoServerTemplate = bean;
});
appContext.subBeansOfType(SaSsoClientTemplate.class, bean -> {
SaSsoClientProcessor.instance.ssoClientTemplate = bean;
});
appContext.subBeansOfType(SaSsoServerConfig.class, bean -> {
SaSsoManager.setServerConfig(bean);
});
appContext.subBeansOfType(SaSsoClientConfig.class, bean -> {
SaSsoManager.setClientConfig(bean);
});
}
/**
* 获取 SSO Server 配置Bean
*/
@Bean
public SaSsoServerConfig getConfig(@Inject(value = "${sa-token.sso-server}", required = false) SaSsoServerConfig ssoConfig) {
return ssoConfig;
}
/**
* 获取 SSO Client 配置Bean
*/
@Bean
public SaSsoClientConfig getClientConfig(@Inject(value = "${sa-token.sso-client}", required = false) SaSsoClientConfig ssoConfig) {
return ssoConfig;
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.sso;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.config.SaSsoClientConfig;
import cn.dev33.satoken.sso.config.SaSsoServerConfig;
import cn.dev33.satoken.sso.processor.SaSsoClientProcessor;
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
import cn.dev33.satoken.sso.template.SaSsoClientTemplate;
import cn.dev33.satoken.sso.template.SaSsoServerTemplate;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* 注入 Sa-Token SSO 所需要的 Bean
*
* @author click33
* @since 1.34.0
*/
@Condition(onClass=SaSsoManager.class)
@Configuration
public class SaSsoBeanInject {
/**
* 注入 Sa-Token SSO Server 端 配置类
*
* @param serverConfig 配置对象
*/
@Condition(onBean = SaSsoServerConfig.class)
@Bean
public void setSaSsoServerConfig(SaSsoServerConfig serverConfig) {
SaSsoManager.setServerConfig(serverConfig);
}
/**
* 注入 Sa-Token SSO Client 端 配置类
*
* @param clientConfig 配置对象
*/
@Condition(onBean = SaSsoClientConfig.class)
@Bean
public void setSaSsoClientConfig(SaSsoClientConfig clientConfig) {
SaSsoManager.setClientConfig(clientConfig);
}
/**
* 注入 SSO 模板代码类 (Server 端)
*
* @param ssoServerTemplate /
*/
@Condition(onBean = SaSsoServerTemplate.class)
@Bean
public void setSaSsoServerTemplate(SaSsoServerTemplate ssoServerTemplate) {
SaSsoServerProcessor.instance.ssoServerTemplate = ssoServerTemplate;
}
/**
* 注入 SSO 模板代码类 (Client 端)
*
* @param ssoClientTemplate /
*/
@Condition(onBean = SaSsoClientTemplate.class)
@Bean
public void setSaSsoClientTemplate(SaSsoClientTemplate ssoClientTemplate) {
SaSsoClientProcessor.instance.ssoClientTemplate = ssoClientTemplate;
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.sso;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.config.SaSsoClientConfig;
import cn.dev33.satoken.sso.config.SaSsoServerConfig;
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 SSO 所需要的 Bean
*
* @author click33
* @since 1.34.0
*/
@Condition(onClass=SaSsoManager.class)
@Configuration
public class SaSsoBeanRegister {
/**
* 获取 SSO Server 端 配置对象
*
* @return 配置对象
*/
@Bean
public SaSsoServerConfig getSaSsoServerConfig(@Inject(value = "${sa-token.sso-server)", required = false) SaSsoServerConfig serverConfig) {
if (serverConfig == null) {
return new SaSsoServerConfig();
} else {
return serverConfig;
}
}
/**
* 获取 SSO Client 端 配置对象
*
* @return 配置对象
*/
@Bean
public SaSsoClientConfig getSaSsoClientConfig(@Inject(value = "${sa-token.sso-client}", required = false) SaSsoClientConfig clientConfig) {
if (clientConfig == null) {
return new SaSsoClientConfig();
} else {
return clientConfig;
}
}
}

View File

@@ -1 +1 @@
solon.plugin=cn.dev33.satoken.solon.XPluginImp
solon.plugin=cn.dev33.satoken.solon.SaSolonPlugin