1
0
mirror of https://gitee.com/dromara/sa-token.git synced 2025-04-05 17:37:53 +08:00

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

This commit is contained in:
noear 2025-02-25 17:06:41 +08:00
parent 028a79abd8
commit 273e4c74db
4 changed files with 237 additions and 54 deletions
sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon

View File

@ -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);
}
}

View File

@ -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;
}
}

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();
}
}