mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 04:35:16 +08:00
refactor: API Key 模块拆分独立插件包:sa-token-apikey
This commit is contained in:
parent
3edac001ce
commit
44c153fd19
@ -179,6 +179,11 @@
|
||||
<artifactId>sa-token-oauth2</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-apikey</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-quick-login</artifactId>
|
||||
|
@ -15,9 +15,6 @@
|
||||
*/
|
||||
package cn.dev33.satoken;
|
||||
|
||||
import cn.dev33.satoken.apikey.SaApiKeyTemplate;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoaderDefaultImpl;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
@ -310,44 +307,6 @@ public class SaManager {
|
||||
return totpTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* ApiKey 数据加载器
|
||||
*/
|
||||
private volatile static SaApiKeyDataLoader apiKeyDataLoader;
|
||||
public static void setSaApiKeyDataLoader(SaApiKeyDataLoader apiKeyDataLoader) {
|
||||
SaManager.apiKeyDataLoader = apiKeyDataLoader;
|
||||
SaTokenEventCenter.doRegisterComponent("SaApiKeyDataLoader", apiKeyDataLoader);
|
||||
}
|
||||
public static SaApiKeyDataLoader getSaApiKeyDataLoader() {
|
||||
if (apiKeyDataLoader == null) {
|
||||
synchronized (SaManager.class) {
|
||||
if (apiKeyDataLoader == null) {
|
||||
SaManager.apiKeyDataLoader = new SaApiKeyDataLoaderDefaultImpl();
|
||||
}
|
||||
}
|
||||
}
|
||||
return apiKeyDataLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* ApiKey 操作类
|
||||
*/
|
||||
private volatile static SaApiKeyTemplate apiKeyTemplate;
|
||||
public static void setSaApiKeyTemplate(SaApiKeyTemplate apiKeyTemplate) {
|
||||
SaManager.apiKeyTemplate = apiKeyTemplate;
|
||||
SaTokenEventCenter.doRegisterComponent("SaApiKeyTemplate", apiKeyTemplate);
|
||||
}
|
||||
public static SaApiKeyTemplate getSaApiKeyTemplate() {
|
||||
if (apiKeyTemplate == null) {
|
||||
synchronized (SaManager.class) {
|
||||
if (apiKeyTemplate == null) {
|
||||
SaManager.apiKeyTemplate = new SaApiKeyTemplate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return apiKeyTemplate;
|
||||
}
|
||||
|
||||
|
||||
// ------------------- StpLogic 相关 -------------------
|
||||
|
||||
|
@ -78,13 +78,6 @@ public @interface SaCheckOr {
|
||||
*/
|
||||
SaCheckDisable[] disable() default {};
|
||||
|
||||
/**
|
||||
* 设定 @SaCheckApiKey,参考 {@link SaCheckApiKey}
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
SaCheckApiKey[] apikey() default {};
|
||||
|
||||
/**
|
||||
* 需要追加抓取的注解 Class (只能填写 Sa-Token 相关注解类型)
|
||||
*
|
||||
|
@ -40,7 +40,7 @@ public class SaCheckOrHandler implements SaAnnotationHandlerInterface<SaCheckOr>
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaCheckOr at, AnnotatedElement element) {
|
||||
_checkMethod(at.login(), at.role(), at.permission(), at.safe(), at.httpBasic(), at.httpDigest(), at.disable(), at.apikey(), at.append(), element);
|
||||
_checkMethod(at.login(), at.role(), at.permission(), at.safe(), at.httpBasic(), at.httpDigest(), at.disable(), at.append(), element);
|
||||
}
|
||||
|
||||
public static void _checkMethod(
|
||||
@ -51,7 +51,6 @@ public class SaCheckOrHandler implements SaAnnotationHandlerInterface<SaCheckOr>
|
||||
SaCheckHttpBasic[] httpBasic,
|
||||
SaCheckHttpDigest[] httpDigest,
|
||||
SaCheckDisable[] disable,
|
||||
SaCheckApiKey[] apikey,
|
||||
Class<? extends Annotation>[] append,
|
||||
AnnotatedElement element
|
||||
) {
|
||||
@ -64,7 +63,6 @@ public class SaCheckOrHandler implements SaAnnotationHandlerInterface<SaCheckOr>
|
||||
annotationList.addAll(Arrays.asList(disable));
|
||||
annotationList.addAll(Arrays.asList(httpBasic));
|
||||
annotationList.addAll(Arrays.asList(httpDigest));
|
||||
annotationList.addAll(Arrays.asList(apikey));
|
||||
for (Class<? extends Annotation> annotationClass : append) {
|
||||
Annotation annotation = SaAnnotationStrategy.instance.getAnnotation.apply(element, annotationClass);
|
||||
if(annotation != null) {
|
||||
|
@ -233,11 +233,6 @@ public class SaTokenConfig implements Serializable {
|
||||
*/
|
||||
public Map<String, SaSignConfig> signMany = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* API Key 相关配置
|
||||
*/
|
||||
public SaApiKeyConfig apiKey = new SaApiKeyConfig();
|
||||
|
||||
/**
|
||||
* @return token 名称 (同时也是: cookie 名称、提交 token 时参数的名称、存储 token 时的 key 前缀)
|
||||
*/
|
||||
@ -902,26 +897,6 @@ public class SaTokenConfig implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* API Key 相关配置
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
public SaApiKeyConfig getApiKey() {
|
||||
return this.apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 API Key 相关配置
|
||||
*
|
||||
* @param apiKey /
|
||||
* @return /
|
||||
*/
|
||||
public SaTokenConfig setApiKey(SaApiKeyConfig apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@ -965,7 +940,6 @@ public class SaTokenConfig implements Serializable {
|
||||
+ ", cookie=" + cookie
|
||||
+ ", sign=" + sign
|
||||
+ ", signMany=" + signMany
|
||||
+ ", apiKey=" + apiKey
|
||||
+ "]";
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,6 @@ public final class SaAnnotationStrategy {
|
||||
annotationHandlerMap.put(SaCheckHttpDigest.class, new SaCheckHttpDigestHandler());
|
||||
annotationHandlerMap.put(SaCheckOr.class, new SaCheckOrHandler());
|
||||
annotationHandlerMap.put(SaCheckSign.class, new SaCheckSignHandler());
|
||||
annotationHandlerMap.put(SaCheckApiKey.class, new SaCheckApiKeyHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,6 +47,13 @@
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sa-Token 整合 API Key -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-apikey</artifactId>
|
||||
<version>${sa-token.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 热刷新 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.pj;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@ -9,8 +10,9 @@ public class SaTokenApiKeyApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SaTokenApiKeyApplication.class, args);
|
||||
System.out.println("\n启动成功,Sa-Token 配置如下:" + SaManager.getConfig());
|
||||
System.out.println("\n测试访问:http://localhost:8081/index.html");
|
||||
System.out.println("启动成功:Sa-Token 配置如下:" + SaManager.getConfig());
|
||||
System.out.println("启动成功:API Key 配置如下:" + SaApiKeyManager.getConfig());
|
||||
System.out.println("测试访问:http://localhost:8081/index.html");
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package com.pj.test;
|
||||
|
||||
import cn.dev33.satoken.apikey.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.apikey.model.ApiKeyModel;
|
||||
import cn.dev33.satoken.apikey.template.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.pj.test;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckApiKey;
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.apikey.annotation.SaCheckApiKey;
|
||||
import cn.dev33.satoken.apikey.model.ApiKeyModel;
|
||||
import cn.dev33.satoken.apikey.template.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
@ -68,12 +68,21 @@ API Key 具有以下特点:
|
||||
|
||||
|
||||
|
||||
### 2、引入依赖
|
||||
在使用 API Key 模块之前,你必须先引入依赖:
|
||||
``` xml
|
||||
<!-- Sa-Token 整合 API Key -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-apikey</artifactId>
|
||||
<version>${sa.top.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
### 2、创建 API Key
|
||||
### 3、创建 API Key
|
||||
|
||||
理解了应用场景后,让我们看看 Sa-Token 为 API Key 提供了哪些方法
|
||||
*(此插件是内嵌到 sa-token-core 核心包中的模块,大家无需再次引入其它依赖,插件直接可用)*:
|
||||
理解了应用场景后,让我们看看 Sa-Token 为 API Key 提供了哪些方法:
|
||||
|
||||
|
||||
``` java
|
||||
@ -117,7 +126,7 @@ List<ApiKeyModel> apiKeyList = SaApiKeyUtil.getApiKeyList(10001);
|
||||
```
|
||||
|
||||
|
||||
### 3、校验 API Key
|
||||
### 4、校验 API Key
|
||||
|
||||
``` java
|
||||
// 校验指定 API Key 是否有效,无效会抛出异常 ApiKeyException
|
||||
@ -181,7 +190,7 @@ public class ApiKeyResourcesController {
|
||||
```
|
||||
|
||||
|
||||
### 4、前端如何提交 API Key?
|
||||
### 5、前端如何提交 API Key?
|
||||
默认情况下,前端可以从任意途径提交 API Key 字符串,只要后端能接受到。
|
||||
|
||||
但是如果后端是通过 `SaApiKeyUtil.currentApiKey()` 方法获取,或者 `@SaCheckApiKey` 注解校验,则需要前端按照一定的格式来提交了:
|
||||
@ -203,7 +212,7 @@ http://AK-NAO6u57zbOWCmLaiVQuVW2tyt3rHpZrXkaQp@localhost:8081/user/getInfo
|
||||
|
||||
|
||||
|
||||
### 5、打开数据库模式
|
||||
### 6、打开数据库模式
|
||||
|
||||
框架默认将所有 API Key 信息保存在缓存中,这可以称之为“缓存模式”,这种模式下,重启缓存库后,数据将丢失。
|
||||
|
||||
@ -244,7 +253,7 @@ public class SaApiKeyDataLoaderImpl implements SaApiKeyDataLoader {
|
||||
|
||||
|
||||
|
||||
### 6、多账号模式使用
|
||||
### 7、多账号模式使用
|
||||
|
||||
如果系统有多套账号表,比如 Admin 和 User,只需要指定不同的命名空间即可:
|
||||
|
||||
|
@ -200,6 +200,7 @@ Maven依赖一直无法加载成功?[参考解决方案](https://sa-token.cc/d
|
||||
├── sa-token-temp-jwt // [插件] Sa-Token 整合 jjwt (临时 Token)
|
||||
├── sa-token-sso // [插件] Sa-Token 实现 SSO 单点登录
|
||||
├── sa-token-oauth2 // [插件] Sa-Token 实现 OAuth2.0 认证
|
||||
├── sa-token-apikey // [插件] Sa-Token 实现 API Key 认证
|
||||
├── sa-token-redisson // [插件] Sa-Token 整合 Redisson (数据缓存插件)
|
||||
├── sa-token-redisx // [插件] Sa-Token 整合 Redisx (数据缓存插件)
|
||||
├── sa-token-serializer-features // [插件] Sa-Token 序列化实现扩展
|
||||
|
@ -33,6 +33,7 @@
|
||||
<module>sa-token-jwt</module>
|
||||
<module>sa-token-sso</module>
|
||||
<module>sa-token-oauth2</module>
|
||||
<module>sa-token-apikey</module>
|
||||
<module>sa-token-redisson</module>
|
||||
<module>sa-token-redisx</module>
|
||||
<module>sa-token-serializer-features</module>
|
||||
|
21
sa-token-plugin/sa-token-apikey/pom.xml
Normal file
21
sa-token-plugin/sa-token-apikey/pom.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>sa-token-plugin</artifactId>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sa-token-apikey</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.apikey;
|
||||
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoaderDefaultImpl;
|
||||
import cn.dev33.satoken.apikey.config.SaApiKeyConfig;
|
||||
import cn.dev33.satoken.apikey.template.SaApiKeyTemplate;
|
||||
import cn.dev33.satoken.listener.SaTokenEventCenter;
|
||||
|
||||
/**
|
||||
* 管理 Sa-Token API Key 所有全局组件
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.43.0
|
||||
*/
|
||||
public class SaApiKeyManager {
|
||||
|
||||
/**
|
||||
* API Key 配置 Bean
|
||||
*/
|
||||
private static volatile SaApiKeyConfig config;
|
||||
public static SaApiKeyConfig getConfig() {
|
||||
if (config == null) {
|
||||
// 初始化默认值
|
||||
synchronized (SaApiKeyManager.class) {
|
||||
if (config == null) {
|
||||
setConfig(new SaApiKeyConfig());
|
||||
}
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
public static void setConfig(SaApiKeyConfig config) {
|
||||
SaApiKeyManager.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* ApiKey 数据加载器
|
||||
*/
|
||||
private volatile static SaApiKeyDataLoader apiKeyDataLoader;
|
||||
public static void setSaApiKeyDataLoader(SaApiKeyDataLoader apiKeyDataLoader) {
|
||||
SaApiKeyManager.apiKeyDataLoader = apiKeyDataLoader;
|
||||
SaTokenEventCenter.doRegisterComponent("SaApiKeyDataLoader", apiKeyDataLoader);
|
||||
}
|
||||
public static SaApiKeyDataLoader getSaApiKeyDataLoader() {
|
||||
if (apiKeyDataLoader == null) {
|
||||
synchronized (SaApiKeyManager.class) {
|
||||
if (apiKeyDataLoader == null) {
|
||||
SaApiKeyManager.apiKeyDataLoader = new SaApiKeyDataLoaderDefaultImpl();
|
||||
}
|
||||
}
|
||||
}
|
||||
return apiKeyDataLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* ApiKey 操作类
|
||||
*/
|
||||
private volatile static SaApiKeyTemplate apiKeyTemplate;
|
||||
public static void setSaApiKeyTemplate(SaApiKeyTemplate apiKeyTemplate) {
|
||||
SaApiKeyManager.apiKeyTemplate = apiKeyTemplate;
|
||||
SaTokenEventCenter.doRegisterComponent("SaApiKeyTemplate", apiKeyTemplate);
|
||||
}
|
||||
public static SaApiKeyTemplate getSaApiKeyTemplate() {
|
||||
if (apiKeyTemplate == null) {
|
||||
synchronized (SaApiKeyManager.class) {
|
||||
if (apiKeyTemplate == null) {
|
||||
SaApiKeyManager.apiKeyTemplate = new SaApiKeyTemplate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return apiKeyTemplate;
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.annotation;
|
||||
package cn.dev33.satoken.apikey.annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
@ -13,11 +13,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.annotation.handler;
|
||||
package cn.dev33.satoken.apikey.annotation.handle;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckApiKey;
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
|
||||
import cn.dev33.satoken.apikey.annotation.SaCheckApiKey;
|
||||
import cn.dev33.satoken.apikey.template.SaApiKeyUtil;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.config;
|
||||
package cn.dev33.satoken.apikey.config;
|
||||
|
||||
/**
|
||||
* Sa-Token API Key 相关配置
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.apikey.loader;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import cn.dev33.satoken.apikey.model.ApiKeyModel;
|
||||
|
||||
/**
|
||||
@ -32,7 +32,7 @@ public interface SaApiKeyDataLoader {
|
||||
* @return /
|
||||
*/
|
||||
default Boolean getIsRecordIndex() {
|
||||
return SaManager.getConfig().getApiKey().getIsRecordIndex();
|
||||
return SaApiKeyManager.getConfig().getIsRecordIndex();
|
||||
}
|
||||
|
||||
/**
|
@ -13,9 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.apikey;
|
||||
package cn.dev33.satoken.apikey.template;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import cn.dev33.satoken.apikey.model.ApiKeyModel;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
@ -94,7 +95,7 @@ public class SaApiKeyTemplate {
|
||||
* @return /
|
||||
*/
|
||||
public ApiKeyModel getApiKeyModelFromDatabase(String apiKey) {
|
||||
return SaManager.getSaApiKeyDataLoader().getApiKeyModelFromDatabase(namespace, apiKey);
|
||||
return SaApiKeyManager.getSaApiKeyDataLoader().getApiKeyModelFromDatabase(namespace, apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -264,7 +265,7 @@ public class SaApiKeyTemplate {
|
||||
* @return /
|
||||
*/
|
||||
public ApiKeyModel createApiKeyModel(Object loginId) {
|
||||
long timeout = SaManager.getConfig().getApiKey().getTimeout();
|
||||
long timeout = SaApiKeyManager.getConfig().getTimeout();
|
||||
long expiresTime = (timeout == SaTokenDao.NEVER_EXPIRE) ? SaTokenDao.NEVER_EXPIRE : System.currentTimeMillis() + timeout * 1000;
|
||||
return createApiKeyModel()
|
||||
.setLoginId(loginId)
|
||||
@ -279,7 +280,7 @@ public class SaApiKeyTemplate {
|
||||
* @return /
|
||||
*/
|
||||
public String randomApiKeyValue() {
|
||||
return SaManager.getConfig().getApiKey().getPrefix() + SaFoxUtil.getRandomString(36);
|
||||
return SaApiKeyManager.getConfig().getPrefix() + SaFoxUtil.getRandomString(36);
|
||||
}
|
||||
|
||||
|
||||
@ -553,7 +554,7 @@ public class SaApiKeyTemplate {
|
||||
* 是否保存索引信息
|
||||
*/
|
||||
public boolean getIsRecordIndex() {
|
||||
return SaManager.getSaApiKeyDataLoader().getIsRecordIndex();
|
||||
return SaApiKeyManager.getSaApiKeyDataLoader().getIsRecordIndex();
|
||||
}
|
||||
|
||||
}
|
@ -13,9 +13,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.apikey;
|
||||
package cn.dev33.satoken.apikey.template;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import cn.dev33.satoken.apikey.model.ApiKeyModel;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
@ -36,7 +36,7 @@ public class SaApiKeyUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static ApiKeyModel getApiKey(String apiKey) {
|
||||
return SaManager.getSaApiKeyTemplate().getApiKey(apiKey);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().getApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,7 +45,7 @@ public class SaApiKeyUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static ApiKeyModel checkApiKey(String apiKey) {
|
||||
return SaManager.getSaApiKeyTemplate().checkApiKey(apiKey);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().checkApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +53,7 @@ public class SaApiKeyUtil {
|
||||
* @param ak /
|
||||
*/
|
||||
public static void saveApiKey(ApiKeyModel ak) {
|
||||
SaManager.getSaApiKeyTemplate().saveApiKey(ak);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().saveApiKey(ak);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,7 +62,7 @@ public class SaApiKeyUtil {
|
||||
* @return LoginId
|
||||
*/
|
||||
public static Object getLoginIdByApiKey(String apiKey) {
|
||||
return SaManager.getSaApiKeyTemplate().getLoginIdByApiKey(apiKey);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().getLoginIdByApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,7 +70,7 @@ public class SaApiKeyUtil {
|
||||
* @param apiKey ApiKey
|
||||
*/
|
||||
public static void deleteApiKey(String apiKey) {
|
||||
SaManager.getSaApiKeyTemplate().deleteApiKey(apiKey);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().deleteApiKey(apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,7 +78,7 @@ public class SaApiKeyUtil {
|
||||
* @param loginId /
|
||||
*/
|
||||
public static void deleteApiKeyByLoginId(Object loginId) {
|
||||
SaManager.getSaApiKeyTemplate().deleteApiKeyByLoginId(loginId);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().deleteApiKeyByLoginId(loginId);
|
||||
}
|
||||
|
||||
// ------- 创建
|
||||
@ -89,7 +89,7 @@ public class SaApiKeyUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static ApiKeyModel createApiKeyModel() {
|
||||
return SaManager.getSaApiKeyTemplate().createApiKeyModel();
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().createApiKeyModel();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,7 +98,7 @@ public class SaApiKeyUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static ApiKeyModel createApiKeyModel(Object loginId) {
|
||||
return SaManager.getSaApiKeyTemplate().createApiKeyModel(loginId);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().createApiKeyModel(loginId);
|
||||
}
|
||||
|
||||
|
||||
@ -110,7 +110,7 @@ public class SaApiKeyUtil {
|
||||
* @param scopes 需要校验的权限列表
|
||||
*/
|
||||
public static boolean hasApiKeyScope(String apiKey, String... scopes) {
|
||||
return SaManager.getSaApiKeyTemplate().hasApiKeyScope(apiKey, scopes);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().hasApiKeyScope(apiKey, scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,7 +119,7 @@ public class SaApiKeyUtil {
|
||||
* @param scopes 需要校验的权限列表
|
||||
*/
|
||||
public static void checkApiKeyScope(String apiKey, String... scopes) {
|
||||
SaManager.getSaApiKeyTemplate().checkApiKeyScope(apiKey, scopes);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().checkApiKeyScope(apiKey, scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,7 +128,7 @@ public class SaApiKeyUtil {
|
||||
* @param scopes 需要校验的权限列表
|
||||
*/
|
||||
public static boolean hasApiKeyScopeOr(String apiKey, String... scopes) {
|
||||
return SaManager.getSaApiKeyTemplate().hasApiKeyScopeOr(apiKey, scopes);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().hasApiKeyScopeOr(apiKey, scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -137,7 +137,7 @@ public class SaApiKeyUtil {
|
||||
* @param scopes 需要校验的权限列表
|
||||
*/
|
||||
public static void checkApiKeyScopeOr(String apiKey, String... scopes) {
|
||||
SaManager.getSaApiKeyTemplate().checkApiKeyScopeOr(apiKey, scopes);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().checkApiKeyScopeOr(apiKey, scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,7 +146,7 @@ public class SaApiKeyUtil {
|
||||
* @param loginId /
|
||||
*/
|
||||
public static boolean isApiKeyLoginId(String apiKey, Object loginId) {
|
||||
return SaManager.getSaApiKeyTemplate().isApiKeyLoginId(apiKey, loginId);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().isApiKeyLoginId(apiKey, loginId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,7 +156,7 @@ public class SaApiKeyUtil {
|
||||
* @param loginId /
|
||||
*/
|
||||
public static void checkApiKeyLoginId(String apiKey, Object loginId) {
|
||||
SaManager.getSaApiKeyTemplate().checkApiKeyLoginId(apiKey, loginId);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().checkApiKeyLoginId(apiKey, loginId);
|
||||
}
|
||||
|
||||
|
||||
@ -166,14 +166,14 @@ public class SaApiKeyUtil {
|
||||
* 数据读取:从请求对象中读取 ApiKey,获取不到返回 null
|
||||
*/
|
||||
public static String readApiKeyValue(SaRequest request) {
|
||||
return SaManager.getSaApiKeyTemplate().readApiKeyValue(request);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().readApiKeyValue(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据读取:从请求对象中读取 ApiKey,并查询到 ApiKeyModel 信息
|
||||
*/
|
||||
public static ApiKeyModel currentApiKey() {
|
||||
return SaManager.getSaApiKeyTemplate().currentApiKey();
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().currentApiKey();
|
||||
}
|
||||
|
||||
|
||||
@ -185,7 +185,7 @@ public class SaApiKeyUtil {
|
||||
* @param session 可填写 null,代表使用 loginId 现场查询
|
||||
*/
|
||||
public static void adjustIndex(Object loginId, SaSession session) {
|
||||
SaManager.getSaApiKeyTemplate().adjustIndex(loginId, session);
|
||||
SaApiKeyManager.getSaApiKeyTemplate().adjustIndex(loginId, session);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -194,7 +194,7 @@ public class SaApiKeyUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static List<ApiKeyModel> getApiKeyList(Object loginId) {
|
||||
return SaManager.getSaApiKeyTemplate().getApiKeyList(loginId);
|
||||
return SaApiKeyManager.getSaApiKeyTemplate().getApiKeyList(loginId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.plugin;
|
||||
|
||||
import cn.dev33.satoken.apikey.annotation.handle.SaCheckApiKeyHandler;
|
||||
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
|
||||
|
||||
/**
|
||||
* SaToken 插件安装:API Key 组件
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.43.0
|
||||
*/
|
||||
public class SaTokenPluginForApiKey implements SaTokenPlugin {
|
||||
|
||||
@Override
|
||||
public void install() {
|
||||
// 安装 API Key 鉴权注解
|
||||
SaAnnotationStrategy.instance.registerAnnotationHandler(new SaCheckApiKeyHandler());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
cn.dev33.satoken.plugin.SaTokenPluginForApiKey
|
@ -49,19 +49,27 @@
|
||||
<artifactId>sa-token-jackson</artifactId>
|
||||
</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>
|
||||
|
||||
<!-- OAuth2.0 (optional) -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-oauth2</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- API Key (optional) -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-apikey</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
@ -17,8 +17,6 @@ package cn.dev33.satoken.spring;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
|
||||
import cn.dev33.satoken.apikey.SaApiKeyTemplate;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
@ -219,26 +217,6 @@ public class SaBeanInject {
|
||||
SaManager.setSaSignTemplate(saSignTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 ApiKey 模块 Bean
|
||||
*
|
||||
* @param apiKeyTemplate /
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaApiKeyTemplate(SaApiKeyTemplate apiKeyTemplate) {
|
||||
SaManager.setSaApiKeyTemplate(apiKeyTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 ApiKey 数据加载器 Bean
|
||||
*
|
||||
* @param apiKeyDataLoader /
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaApiKeyDataLoader(SaApiKeyDataLoader apiKeyDataLoader) {
|
||||
SaManager.setSaApiKeyDataLoader(apiKeyDataLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 TOTP 算法 Bean
|
||||
*
|
||||
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.spring.apikey;
|
||||
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import cn.dev33.satoken.apikey.config.SaApiKeyConfig;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.apikey.template.SaApiKeyTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
|
||||
/**
|
||||
* 注入 Sa-Token API Key 所需要的 Bean
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.43.0
|
||||
*/
|
||||
@ConditionalOnClass(SaApiKeyManager.class)
|
||||
public class SaApiKeyBeanInject {
|
||||
|
||||
/**
|
||||
* 注入 API Key 配置对象
|
||||
*
|
||||
* @param saApiKeyConfig 配置对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaOAuth2Config(SaApiKeyConfig saApiKeyConfig) {
|
||||
SaApiKeyManager.setConfig(saApiKeyConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 API Key 模版方法 Bean
|
||||
*
|
||||
* @param apiKeyTemplate /
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaApiKeyTemplate(SaApiKeyTemplate apiKeyTemplate) {
|
||||
SaApiKeyManager.setSaApiKeyTemplate(apiKeyTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入自定义的 API Key 数据加载器 Bean
|
||||
*
|
||||
* @param apiKeyDataLoader /
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaApiKeyDataLoader(SaApiKeyDataLoader apiKeyDataLoader) {
|
||||
SaApiKeyManager.setSaApiKeyDataLoader(apiKeyDataLoader);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.spring.apikey;
|
||||
|
||||
import cn.dev33.satoken.apikey.SaApiKeyManager;
|
||||
import cn.dev33.satoken.apikey.config.SaApiKeyConfig;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 注册 Sa-Token API Key 所需要的 Bean
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.34.0
|
||||
*/
|
||||
@ConditionalOnClass(SaApiKeyManager.class)
|
||||
public class SaApiKeyBeanRegister {
|
||||
|
||||
/**
|
||||
* 获取 API Key 配置对象
|
||||
* @return 配置对象
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sa-token.api-key")
|
||||
public SaApiKeyConfig getSaApiKeyConfig() {
|
||||
return new SaApiKeyConfig();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
/**
|
||||
* sa-token-apikey 模块自动化配置(只有引入了 sa-token-apikey 模块后,此包下的代码才会开始工作)
|
||||
*/
|
||||
package cn.dev33.satoken.spring.apikey;
|
Loading…
Reference in New Issue
Block a user