mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-08-20 00:44:30 +08:00
sa-token-solon-plugin 优化 sa-token 适配,保持与 spirngboot 类似的适配体验
This commit is contained in:
parent
cdad8e7245
commit
561c40c3ba
@ -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);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.SaOAuth2AutoConfigure;
|
||||
import cn.dev33.satoken.solon.sso.SaSsoAutoConfigure;
|
||||
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(SaSsoAutoConfigure.class);
|
||||
|
||||
//sa-oauth2
|
||||
context.beanMake(SaOAuth2AutoConfigure.class);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -32,22 +32,21 @@ import org.noear.solon.core.AppContext;
|
||||
@Condition(onClass = SaOAuth2Manager.class)
|
||||
@Configuration
|
||||
public class SaOAuth2AutoConfigure {
|
||||
@Condition(onBean = SaOAuth2Template.class)
|
||||
@Bean
|
||||
public void init(AppContext appContext) throws Throwable {
|
||||
appContext.subBeansOfType(SaOAuth2Template.class, bean -> {
|
||||
SaOAuth2Manager.setTemplate(bean);
|
||||
});
|
||||
|
||||
appContext.subBeansOfType(SaOAuth2ServerConfig.class, bean -> {
|
||||
SaOAuth2Manager.setServerConfig(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 oAuth2Config) {
|
||||
return oAuth2Config;
|
||||
public SaOAuth2ServerConfig getConfig(@Inject(value = "${sa-token.oauth2-server}", required = false) SaOAuth2ServerConfig serverConfig) {
|
||||
if (serverConfig != null) {
|
||||
SaOAuth2Manager.setServerConfig(serverConfig);
|
||||
}
|
||||
|
||||
return serverConfig;
|
||||
}
|
||||
}
|
||||
@ -36,36 +36,37 @@ import org.noear.solon.core.AppContext;
|
||||
@Condition(onClass = SaSsoManager.class)
|
||||
@Configuration
|
||||
public class SaSsoAutoConfigure {
|
||||
@Condition(onBean = SaSsoServerTemplate.class)
|
||||
@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;
|
||||
});
|
||||
public void serverTemplate(SaSsoServerTemplate bean) {
|
||||
SaSsoServerProcessor.instance.ssoServerTemplate = bean;
|
||||
}
|
||||
|
||||
appContext.subBeansOfType(SaSsoServerConfig.class, bean -> {
|
||||
SaSsoManager.setServerConfig(bean);
|
||||
});
|
||||
appContext.subBeansOfType(SaSsoClientConfig.class, bean -> {
|
||||
SaSsoManager.setClientConfig(bean);
|
||||
});
|
||||
@Condition(onBean = SaSsoClientTemplate.class)
|
||||
@Bean
|
||||
public void clientTemplate(SaSsoClientTemplate bean) {
|
||||
SaSsoClientProcessor.instance.ssoClientTemplate = bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 SSO Server 配置Bean
|
||||
*/
|
||||
@Bean
|
||||
public SaSsoServerConfig getConfig(@Inject(value = "${sa-token.sso-server}", required = false) SaSsoServerConfig ssoConfig) {
|
||||
return ssoConfig;
|
||||
public SaSsoServerConfig getServerConfig(@Inject(value = "${sa-token.sso-server}", required = false) SaSsoServerConfig serverConfig) {
|
||||
if (serverConfig != null) {
|
||||
SaSsoManager.setServerConfig(serverConfig);
|
||||
}
|
||||
return serverConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 SSO Client 配置Bean
|
||||
*/
|
||||
@Bean
|
||||
public SaSsoClientConfig getClientConfig(@Inject(value = "${sa-token.sso-client}", required = false) SaSsoClientConfig ssoConfig) {
|
||||
return ssoConfig;
|
||||
public SaSsoClientConfig getClientConfig(@Inject(value = "${sa-token.sso-client}", required = false) SaSsoClientConfig clientConfig) {
|
||||
if (clientConfig != null) {
|
||||
SaSsoManager.setClientConfig(clientConfig);
|
||||
}
|
||||
return clientConfig;
|
||||
}
|
||||
}
|
||||
@ -1 +1 @@
|
||||
solon.plugin=cn.dev33.satoken.solon.XPluginImp
|
||||
solon.plugin=cn.dev33.satoken.solon.SaSolonPlugin
|
||||
Loading…
Reference in New Issue
Block a user