适配 springboot3
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-starter</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-spring-boot-autoconfig</name>
|
||||
<artifactId>sa-token-spring-boot-autoconfig</artifactId>
|
||||
<description>sa-token-spring-boot-autoconfig</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- spring-boot-starter (optional) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- config (optional) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- OAuth2.0 (optional) -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-oauth2</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- SSO (optional) -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-sso</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package cn.dev33.satoken.error;
|
||||
|
||||
/**
|
||||
* 定义 sa-token-spring-boot-starter 所有异常细分状态码
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-10-30
|
||||
*/
|
||||
public interface SaSpringBootErrorCode {
|
||||
|
||||
/** 企图在非 Web 上下文获取 Request、Response 等对象 */
|
||||
public static final int CODE_20101 = 20101;
|
||||
|
||||
/** 对象转 JSON 字符串失败 */
|
||||
public static final int CODE_20103 = 20103;
|
||||
|
||||
/** JSON 字符串转 Map 失败 */
|
||||
public static final int CODE_20104 = 20104;
|
||||
|
||||
/** 默认的 Filter 异常处理函数 */
|
||||
public static final int CODE_20105 = 20105;
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* sa-token集成SpringBoot的各个组件
|
||||
*/
|
||||
package cn.dev33.satoken;
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package cn.dev33.satoken.spring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.basic.SaBasicTemplate;
|
||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
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.sign.SaSignTemplate;
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.temp.SaTempInterface;
|
||||
|
||||
/**
|
||||
* 注入Sa-Token所需要的Bean
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaBeanInject {
|
||||
|
||||
/**
|
||||
* 组件注入
|
||||
* <p> 为确保 Log 组件正常打印,必须将 SaLog 和 SaTokenConfig 率先初始化 </p>
|
||||
*
|
||||
* @param saTokenConfig 配置对象
|
||||
*/
|
||||
public SaBeanInject(
|
||||
@Autowired(required = false) SaLog log,
|
||||
@Autowired(required = false) SaTokenConfig saTokenConfig
|
||||
){
|
||||
if(log != null) {
|
||||
SaManager.setLog(log);
|
||||
}
|
||||
if(saTokenConfig != null) {
|
||||
SaManager.setConfig(saTokenConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入持久化Bean
|
||||
*
|
||||
* @param saTokenDao SaTokenDao对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenDao(SaTokenDao saTokenDao) {
|
||||
SaManager.setSaTokenDao(saTokenDao);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入权限认证Bean
|
||||
*
|
||||
* @param stpInterface StpInterface对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setStpInterface(StpInterface stpInterface) {
|
||||
SaManager.setStpInterface(stpInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入上下文Bean
|
||||
*
|
||||
* @param saTokenContext SaTokenContext对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenContext(SaTokenContext saTokenContext) {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入二级上下文Bean
|
||||
*
|
||||
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入侦听器Bean
|
||||
*
|
||||
* @param listenerList 侦听器集合
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenListener(List<SaTokenListener> listenerList) {
|
||||
SaTokenEventCenter.registerListenerList(listenerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入临时令牌验证模块 Bean
|
||||
*
|
||||
* @param saTemp saTemp对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTemp(SaTempInterface saTemp) {
|
||||
SaManager.setSaTemp(saTemp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 Same-Token 模块 Bean
|
||||
*
|
||||
* @param saSameTemplate saSameTemplate对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaIdTemplate(SaSameTemplate saSameTemplate) {
|
||||
SaManager.setSaSameTemplate(saSameTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token Http Basic 认证模块
|
||||
*
|
||||
* @param saBasicTemplate saBasicTemplate对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaBasicTemplate(SaBasicTemplate saBasicTemplate) {
|
||||
SaBasicUtil.saBasicTemplate = saBasicTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 JSON 转换器 Bean
|
||||
*
|
||||
* @param saJsonTemplate JSON 转换器
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaJsonTemplate(SaJsonTemplate saJsonTemplate) {
|
||||
SaManager.setSaJsonTemplate(saJsonTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 参数签名 Bean
|
||||
*
|
||||
* @param saSignTemplate 参数签名 Bean
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaSignTemplate(SaSignTemplate saSignTemplate) {
|
||||
SaManager.setSaSignTemplate(saSignTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 StpLogic
|
||||
* @param stpLogic /
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setStpLogic(StpLogic stpLogic) {
|
||||
StpUtil.setStpLogic(stpLogic);
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用自动注入特性,获取Spring框架内部使用的路由匹配器
|
||||
*
|
||||
* @param pathMatcher 要设置的 pathMatcher
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
@Qualifier("mvcPathMatcher")
|
||||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaPathMatcherHolder.setPathMatcher(pathMatcher);
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
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.json.SaJsonTemplate;
|
||||
import cn.dev33.satoken.spring.json.SaJsonTemplateForJackson;
|
||||
|
||||
/**
|
||||
* 注册Sa-Token所需要的Bean
|
||||
* <p> Bean 的注册与注入应该分开在两个文件中,否则在某些场景下会造成循环依赖
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaBeanRegister {
|
||||
|
||||
/**
|
||||
* 获取配置Bean
|
||||
*
|
||||
* @return 配置对象
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sa-token")
|
||||
public SaTokenConfig getSaTokenConfig() {
|
||||
return new SaTokenConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 json 转换器 Bean (Jackson版)
|
||||
*
|
||||
* @return json 转换器 Bean (Jackson版)
|
||||
*/
|
||||
@Bean
|
||||
public SaJsonTemplate getSaJsonTemplateForJackson() {
|
||||
return new SaJsonTemplateForJackson();
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package cn.dev33.satoken.spring;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaPathMatcherHolder {
|
||||
|
||||
private SaPathMatcherHolder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由匹配器
|
||||
*/
|
||||
public static PathMatcher pathMatcher;
|
||||
|
||||
/**
|
||||
* 获取路由匹配器
|
||||
* @return 路由匹配器
|
||||
*/
|
||||
public static PathMatcher getPathMatcher() {
|
||||
if(pathMatcher == null) {
|
||||
pathMatcher = new AntPathMatcher();
|
||||
}
|
||||
return pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入路由匹配器
|
||||
* @param pathMatcher 路由匹配器
|
||||
*/
|
||||
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaPathMatcherHolder.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package cn.dev33.satoken.spring.json;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import cn.dev33.satoken.error.SaSpringBootErrorCode;
|
||||
import cn.dev33.satoken.exception.SaJsonConvertException;
|
||||
import cn.dev33.satoken.json.SaJsonTemplate;
|
||||
|
||||
/**
|
||||
* JSON 转换器, Jackson 版实现
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-4-26
|
||||
*/
|
||||
public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
||||
|
||||
/**
|
||||
* 底层 Mapper 对象
|
||||
*/
|
||||
public ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 将任意对象转换为 json 字符串
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return 转换后的 json 字符串
|
||||
*/
|
||||
@Override
|
||||
public String toJsonString(Object obj) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SaJsonConvertException(e).setCode(SaSpringBootErrorCode.CODE_20103);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 json 字符串解析为 Map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> parseJsonToMap(String jsonStr) {
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
|
||||
return map;
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SaJsonConvertException(e).setCode(SaSpringBootErrorCode.CODE_20104);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package cn.dev33.satoken.spring.oauth2;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
|
||||
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
|
||||
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
|
||||
import cn.dev33.satoken.oauth2.logic.SaOAuth2Template;
|
||||
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token-OAuth2 所需要的Bean
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@ConditionalOnClass(SaOAuth2Manager.class)
|
||||
public class SaOAuth2BeanInject {
|
||||
|
||||
/**
|
||||
* 注入OAuth2配置Bean
|
||||
*
|
||||
* @param saOAuth2Config 配置对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaOAuth2Config(SaOAuth2Config saOAuth2Config) {
|
||||
SaOAuth2Manager.setConfig(saOAuth2Config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入代码模板Bean
|
||||
*
|
||||
* @param saOAuth2Template 代码模板Bean
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaOAuth2Interface(SaOAuth2Template saOAuth2Template) {
|
||||
SaOAuth2Util.saOAuth2Template = saOAuth2Template;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package cn.dev33.satoken.spring.oauth2;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
|
||||
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
|
||||
|
||||
/**
|
||||
* 注册 Sa-Token-OAuth2 所需要的Bean
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@ConditionalOnClass(SaOAuth2Manager.class)
|
||||
public class SaOAuth2BeanRegister {
|
||||
|
||||
/**
|
||||
* 获取OAuth2配置Bean
|
||||
* @return 配置对象
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sa-token.oauth2")
|
||||
public SaOAuth2Config getSaOAuth2Config() {
|
||||
return new SaOAuth2Config();
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Sa-Token-OAuth2 模块自动化配置(只有引入了Sa-Token-OAuth2模块后,此包下的代码才会开始工作)
|
||||
*/
|
||||
package cn.dev33.satoken.spring.oauth2;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package cn.dev33.satoken.spring.sso;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
|
||||
import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.sso.SaSsoManager;
|
||||
import cn.dev33.satoken.sso.SaSsoProcessor;
|
||||
import cn.dev33.satoken.sso.SaSsoTemplate;
|
||||
import cn.dev33.satoken.sso.SaSsoUtil;
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token-SSO 所需要的Bean
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@ConditionalOnClass(SaSsoManager.class)
|
||||
public class SaSsoBeanInject {
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token-SSO 配置Bean
|
||||
*
|
||||
* @param saSsoConfig 配置对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaOAuth2Config(SaSsoConfig saSsoConfig) {
|
||||
SaSsoManager.setConfig(saSsoConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token-SSO 单点登录模块 Bean
|
||||
*
|
||||
* @param ssoTemplate saSsoTemplate对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaSsoTemplate(SaSsoTemplate ssoTemplate) {
|
||||
SaSsoUtil.ssoTemplate = ssoTemplate;
|
||||
SaSsoProcessor.instance.ssoTemplate = ssoTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package cn.dev33.satoken.spring.sso;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.sso.SaSsoManager;
|
||||
|
||||
/**
|
||||
* 注册 Sa-Token-SSO 所需要的Bean
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@ConditionalOnClass(SaSsoManager.class)
|
||||
public class SaSsoBeanRegister {
|
||||
|
||||
/**
|
||||
* 获取 SSO 配置Bean
|
||||
* @return 配置对象
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sa-token.sso")
|
||||
public SaSsoConfig getSaSsoConfig() {
|
||||
return new SaSsoConfig();
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Sa-Token-SSO 模块自动化配置(只有引入了 sa-token-sso 模块后,此包下的代码才会开始工作)
|
||||
*/
|
||||
package cn.dev33.satoken.spring.sso;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.dev33.satoken.spring.SaBeanRegister,\
|
||||
cn.dev33.satoken.spring.SaBeanInject,\
|
||||
cn.dev33.satoken.spring.sso.SaSsoBeanRegister,\
|
||||
cn.dev33.satoken.spring.sso.SaSsoBeanInject,\
|
||||
cn.dev33.satoken.spring.oauth2.SaOAuth2BeanRegister,\
|
||||
cn.dev33.satoken.spring.oauth2.SaOAuth2BeanInject
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
cn.dev33.satoken.spring.SaBeanRegister
|
||||
cn.dev33.satoken.spring.SaBeanInject
|
||||
cn.dev33.satoken.spring.sso.SaSsoBeanRegister
|
||||
cn.dev33.satoken.spring.sso.SaSsoBeanInject
|
||||
cn.dev33.satoken.spring.oauth2.SaOAuth2BeanRegister
|
||||
cn.dev33.satoken.spring.oauth2.SaOAuth2BeanInject
|
||||
Reference in New Issue
Block a user