From 15027bbe095234aa23f9dd867cb1ac618b275adb Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Wed, 7 Jun 2023 14:16:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=89=80=E6=9C=89=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E6=8A=BD=E7=A6=BB=E4=B8=BA=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SaCheckElementAnnotationFunction.java | 33 +++++ .../SaCheckMethodAnnotationFunction.java | 33 +++++ .../strategy/SaCheckOrAnnotationFunction.java | 34 +++++ .../fun/strategy/SaCreateSessionFunction.java | 34 +++++ .../strategy/SaCreateStpLogicFunction.java | 34 +++++ .../fun/strategy/SaCreateTokenFunction.java | 32 +++++ .../SaGenerateUniqueTokenFunction.java | 7 +- .../fun/strategy/SaGetAnnotationFunction.java | 34 +++++ .../fun/strategy/SaHasElementFunction.java | 33 +++++ .../SaIsAnnotationPresentFunction.java | 34 +++++ .../cn/dev33/satoken/strategy/SaStrategy.java | 136 ++++++++---------- .../main/java/com/pj/SaTokenApplication.java | 8 ++ sa-token-doc/fun/plugin-dev.md | 2 +- sa-token-doc/up/many-account.md | 2 +- sa-token-doc/up/token-style.md | 2 +- 15 files changed, 376 insertions(+), 82 deletions(-) create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckElementAnnotationFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckMethodAnnotationFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckOrAnnotationFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateSessionFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateStpLogicFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateTokenFunction.java rename sa-token-core/src/main/java/cn/dev33/satoken/fun/{ => strategy}/SaGenerateUniqueTokenFunction.java (88%) create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGetAnnotationFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaHasElementFunction.java create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaIsAnnotationPresentFunction.java diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckElementAnnotationFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckElementAnnotationFunction.java new file mode 100644 index 00000000..97a67c2e --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckElementAnnotationFunction.java @@ -0,0 +1,33 @@ +/* + * 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.fun.strategy; + +import java.lang.reflect.AnnotatedElement; +import java.util.function.Consumer; + +/** + * 函数式接口:对一个 [元素] 对象进行注解校验 (注解鉴权内部实现) + * + *

参数:element元素

+ *

返回:无

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCheckElementAnnotationFunction extends Consumer { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckMethodAnnotationFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckMethodAnnotationFunction.java new file mode 100644 index 00000000..8bcd4318 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckMethodAnnotationFunction.java @@ -0,0 +1,33 @@ +/* + * 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.fun.strategy; + +import java.lang.reflect.Method; +import java.util.function.Consumer; + +/** + * 函数式接口:对一个 [Method] 对象进行注解校验 (注解鉴权内部实现) + * + *

参数:Method句柄

+ *

返回:无

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCheckMethodAnnotationFunction extends Consumer { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckOrAnnotationFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckOrAnnotationFunction.java new file mode 100644 index 00000000..c6f6ad54 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCheckOrAnnotationFunction.java @@ -0,0 +1,34 @@ +/* + * 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.fun.strategy; + +import cn.dev33.satoken.annotation.SaCheckOr; + +import java.util.function.Consumer; + +/** + * 函数式接口:对一个 @SaCheckOr 进行注解校验 + * + *

参数:SaCheckOr 注解的实例

+ *

返回:无

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCheckOrAnnotationFunction extends Consumer { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateSessionFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateSessionFunction.java new file mode 100644 index 00000000..0963edff --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateSessionFunction.java @@ -0,0 +1,34 @@ +/* + * 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.fun.strategy; + +import cn.dev33.satoken.session.SaSession; + +import java.util.function.Function; + +/** + * 函数式接口:创建 SaSession 的策略 + * + *

参数:SessionId

+ *

返回:SaSession对象

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCreateSessionFunction extends Function { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateStpLogicFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateStpLogicFunction.java new file mode 100644 index 00000000..2c3c75d6 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateStpLogicFunction.java @@ -0,0 +1,34 @@ +/* + * 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.fun.strategy; + +import cn.dev33.satoken.stp.StpLogic; + +import java.util.function.Function; + +/** + * 函数式接口:创建 StpLogic 的算法 + * + *

参数:账号体系标识

+ *

返回:创建好的 StpLogic 对象

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCreateStpLogicFunction extends Function { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateTokenFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateTokenFunction.java new file mode 100644 index 00000000..f85c40bb --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaCreateTokenFunction.java @@ -0,0 +1,32 @@ +/* + * 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.fun.strategy; + +import java.util.function.BiFunction; + +/** + * 函数式接口:创建 token 的策略 + * + *

参数:账号 id、账号类型

+ *

返回:token 值

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaCreateTokenFunction extends BiFunction { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/SaGenerateUniqueTokenFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGenerateUniqueTokenFunction.java similarity index 88% rename from sa-token-core/src/main/java/cn/dev33/satoken/fun/SaGenerateUniqueTokenFunction.java rename to sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGenerateUniqueTokenFunction.java index 031ec5df..d1a72f0f 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/fun/SaGenerateUniqueTokenFunction.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGenerateUniqueTokenFunction.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package cn.dev33.satoken.fun; +package cn.dev33.satoken.fun.strategy; import java.util.function.Function; import java.util.function.Supplier; @@ -21,8 +21,11 @@ import java.util.function.Supplier; /** * 生成唯一式 token 的函数式接口,方便开发者进行 lambda 表达式风格调用 * + *

参数:元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数

+ *

返回:生成的token

+ * * @author click33 - * @since 1.34.0 + * @since 1.35.0 */ @FunctionalInterface public interface SaGenerateUniqueTokenFunction { diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGetAnnotationFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGetAnnotationFunction.java new file mode 100644 index 00000000..6908b639 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaGetAnnotationFunction.java @@ -0,0 +1,34 @@ +/* + * 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.fun.strategy; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.util.function.BiFunction; + +/** + * 函数式接口:从元素上获取注解 + * + *

参数:element元素,要获取的注解类型

+ *

返回:注解对象

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaGetAnnotationFunction extends BiFunction , Annotation> { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaHasElementFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaHasElementFunction.java new file mode 100644 index 00000000..3ee75089 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaHasElementFunction.java @@ -0,0 +1,33 @@ +/* + * 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.fun.strategy; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * 函数式接口:判断集合中是否包含指定元素(模糊匹配) + * + *

参数:集合、元素

+ *

返回:是否包含

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaHasElementFunction extends BiFunction, String, Boolean> { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaIsAnnotationPresentFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaIsAnnotationPresentFunction.java new file mode 100644 index 00000000..88fe7072 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaIsAnnotationPresentFunction.java @@ -0,0 +1,34 @@ +/* + * 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.fun.strategy; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.function.BiFunction; + +/** + * 函数式接口:判断一个 Method 或其所属 Class 是否包含指定注解 + * + *

参数:Method、注解

+ *

返回:是否包含

+ * + * @author click33 + * @since 1.35.0 + */ +@FunctionalInterface +public interface SaIsAnnotationPresentFunction extends BiFunction, Boolean> { + +} \ No newline at end of file diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java b/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java index 4b50b86e..ee1afc75 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java @@ -19,20 +19,16 @@ import cn.dev33.satoken.SaManager; import cn.dev33.satoken.annotation.*; import cn.dev33.satoken.basic.SaBasicUtil; import cn.dev33.satoken.exception.SaTokenException; -import cn.dev33.satoken.fun.SaGenerateUniqueTokenFunction; +import cn.dev33.satoken.fun.strategy.SaGenerateUniqueTokenFunction; +import cn.dev33.satoken.fun.strategy.*; import cn.dev33.satoken.session.SaSession; import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.util.SaFoxUtil; import cn.dev33.satoken.util.SaTokenConsts; -import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedElement; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.UUID; -import java.util.function.BiFunction; -import java.util.function.Consumer; import java.util.function.Function; /** @@ -42,7 +38,7 @@ import java.util.function.Function; *

*
 	// SaStrategy全局单例,所有方法都用以下形式重写 
-	SaStrategy.me.setCreateToken((loginId, loginType) -》 {
+	SaStrategy.instance.setCreateToken((loginId, loginType) -》 {
 		// 自定义Token生成的算法 
 		return "xxxx";
 	});
@@ -65,10 +61,9 @@ public final class SaStrategy {
 	// ----------------------- 所有策略
 	
 	/**
-	 * 创建 Token 的策略 
-	 * 

参数 [ 账号id, 账号类型 ] + * 创建 Token 的策略 */ - public BiFunction createToken = (loginId, loginType) -> { + public SaCreateTokenFunction createToken = (loginId, loginType) -> { // 根据配置的tokenStyle生成不同风格的token String tokenStyle = SaManager.getConfig().getTokenStyle(); @@ -106,18 +101,16 @@ public final class SaStrategy { }; /** - * 创建 Session 的策略 - *

参数 [SessionId] + * 创建 Session 的策略 */ - public Function createSession = (sessionId) -> { + public SaCreateSessionFunction createSession = (sessionId) -> { return new SaSession(sessionId); }; /** - * 判断:集合中是否包含指定元素(模糊匹配) - *

参数 [集合, 元素] + * 判断:集合中是否包含指定元素(模糊匹配) */ - public BiFunction, String, Boolean> hasElement = (list, element) -> { + public SaHasElementFunction hasElement = (list, element) -> { // 空集合直接返回false if(list == null || list.size() == 0) { @@ -141,10 +134,9 @@ public final class SaStrategy { }; /** - * 对一个 [Method] 对象进行注解校验 (注解鉴权内部实现) - *

参数 [Method句柄] + * 对一个 [Method] 对象进行注解校验 (注解鉴权内部实现) */ - public Consumer checkMethodAnnotation = (method) -> { + public SaCheckMethodAnnotationFunction checkMethodAnnotation = (method) -> { // 先校验 Method 所属 Class 上的注解 instance.checkElementAnnotation.accept(method.getDeclaringClass()); @@ -154,49 +146,48 @@ public final class SaStrategy { }; /** - * 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现) - *

参数 [element元素] + * 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现) */ - public Consumer checkElementAnnotation = (target) -> { + public SaCheckElementAnnotationFunction checkElementAnnotation = (element) -> { // 校验 @SaCheckLogin 注解 - SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.instance.getAnnotation.apply(target, SaCheckLogin.class); + SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.instance.getAnnotation.apply(element, SaCheckLogin.class); if(checkLogin != null) { SaManager.getStpLogic(checkLogin.type(), false).checkByAnnotation(checkLogin); } // 校验 @SaCheckRole 注解 - SaCheckRole checkRole = (SaCheckRole) SaStrategy.instance.getAnnotation.apply(target, SaCheckRole.class); + SaCheckRole checkRole = (SaCheckRole) SaStrategy.instance.getAnnotation.apply(element, SaCheckRole.class); if(checkRole != null) { SaManager.getStpLogic(checkRole.type(), false).checkByAnnotation(checkRole); } // 校验 @SaCheckPermission 注解 - SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.instance.getAnnotation.apply(target, SaCheckPermission.class); + SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.instance.getAnnotation.apply(element, SaCheckPermission.class); if(checkPermission != null) { SaManager.getStpLogic(checkPermission.type(), false).checkByAnnotation(checkPermission); } // 校验 @SaCheckSafe 注解 - SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.instance.getAnnotation.apply(target, SaCheckSafe.class); + SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.instance.getAnnotation.apply(element, SaCheckSafe.class); if(checkSafe != null) { SaManager.getStpLogic(checkSafe.type(), false).checkByAnnotation(checkSafe); } // 校验 @SaCheckDisable 注解 - SaCheckDisable checkDisable = (SaCheckDisable) SaStrategy.instance.getAnnotation.apply(target, SaCheckDisable.class); + SaCheckDisable checkDisable = (SaCheckDisable) SaStrategy.instance.getAnnotation.apply(element, SaCheckDisable.class); if(checkDisable != null) { SaManager.getStpLogic(checkDisable.type(), false).checkByAnnotation(checkDisable); } // 校验 @SaCheckBasic 注解 - SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.instance.getAnnotation.apply(target, SaCheckBasic.class); + SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.instance.getAnnotation.apply(element, SaCheckBasic.class); if(checkBasic != null) { SaBasicUtil.check(checkBasic.realm(), checkBasic.account()); } // 校验 @SaCheckOr 注解 - SaCheckOr checkOr = (SaCheckOr) SaStrategy.instance.getAnnotation.apply(target, SaCheckOr.class); + SaCheckOr checkOr = (SaCheckOr) SaStrategy.instance.getAnnotation.apply(element, SaCheckOr.class); if(checkOr != null) { SaStrategy.instance.checkOrAnnotation.accept(checkOr); } @@ -204,9 +195,8 @@ public final class SaStrategy { /** * 对一个 @SaCheckOr 进行注解校验 - *

参数 [SaCheckOr 注解的实例] */ - public Consumer checkOrAnnotation = (at) -> { + public SaCheckOrAnnotationFunction checkOrAnnotation = (at) -> { // 记录校验过程中所有的异常 List errorList = new ArrayList<>(); @@ -290,26 +280,23 @@ public final class SaStrategy { }; /** - * 从元素上获取注解 - *

参数 [element元素,要获取的注解类型] + * 从元素上获取注解 */ - public BiFunction , Annotation> getAnnotation = (element, annotationClass)->{ + public SaGetAnnotationFunction getAnnotation = (element, annotationClass)->{ // 默认使用jdk的注解处理器 return element.getAnnotation(annotationClass); }; /** - * 判断一个 Method 或其所属 Class 是否包含指定注解 - *

参数 [Method, 注解] + * 判断一个 Method 或其所属 Class 是否包含指定注解 */ - public BiFunction, Boolean> isAnnotationPresent = (method, annotationClass) -> { + public SaIsAnnotationPresentFunction isAnnotationPresent = (method, annotationClass) -> { return instance.getAnnotation.apply(method, annotationClass) != null || instance.getAnnotation.apply(method.getDeclaringClass(), annotationClass) != null; }; /** * 生成唯一式 token 的算法 - *

参数 [元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数] */ public SaGenerateUniqueTokenFunction generateUniqueToken = (elementName, maxTryTimes, createTokenFunction, checkTokenFunction) -> { @@ -339,10 +326,8 @@ public final class SaStrategy { /** * 创建 StpLogic 的算法 - * - *

参数 [ 账号体系标识 ] */ - public Function createStpLogic = (loginType) -> { + public SaCreateStpLogicFunction createStpLogic = (loginType) -> { return new StpLogic(loginType); }; @@ -350,56 +335,56 @@ public final class SaStrategy { // ----------------------- 重写策略 set连缀风格 /** - * 重写创建 Token 的策略 - *

参数 [账号id, 账号类型] + * 重写创建 Token 的策略 + * * @param createToken / - * @return 对象自身 + * @return / */ - public SaStrategy setCreateToken(BiFunction createToken) { + public SaStrategy setCreateToken(SaCreateTokenFunction createToken) { this.createToken = createToken; return this; } /** - * 重写创建 Session 的策略 - *

参数 [SessionId] + * 重写创建 Session 的策略 + * * @param createSession / - * @return 对象自身 + * @return / */ - public SaStrategy setCreateSession(Function createSession) { + public SaStrategy setCreateSession(SaCreateSessionFunction createSession) { this.createSession = createSession; return this; } /** - * 判断:集合中是否包含指定元素(模糊匹配) - *

参数 [集合, 元素] + * 判断:集合中是否包含指定元素(模糊匹配) + * * @param hasElement / - * @return 对象自身 + * @return / */ - public SaStrategy setHasElement(BiFunction, String, Boolean> hasElement) { + public SaStrategy setHasElement(SaHasElementFunction hasElement) { this.hasElement = hasElement; return this; } /** - * 对一个 [Method] 对象进行注解校验 (注解鉴权内部实现) - *

参数 [Method句柄] + * 对一个 [Method] 对象进行注解校验 (注解鉴权内部实现) + * * @param checkMethodAnnotation / - * @return 对象自身 + * @return / */ - public SaStrategy setCheckMethodAnnotation(Consumer checkMethodAnnotation) { + public SaStrategy setCheckMethodAnnotation(SaCheckMethodAnnotationFunction checkMethodAnnotation) { this.checkMethodAnnotation = checkMethodAnnotation; return this; } /** - * 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现) - *

参数 [element元素] + * 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现) + * * @param checkElementAnnotation / - * @return 对象自身 + * @return / */ - public SaStrategy setCheckElementAnnotation(Consumer checkElementAnnotation) { + public SaStrategy setCheckElementAnnotation(SaCheckElementAnnotationFunction checkElementAnnotation) { this.checkElementAnnotation = checkElementAnnotation; return this; } @@ -409,41 +394,40 @@ public final class SaStrategy { *

参数 [SaCheckOr 注解的实例] * * @param checkOrAnnotation / - * @return 对象自身 + * @return / */ - public SaStrategy setCheckOrAnnotation(Consumer checkOrAnnotation) { + public SaStrategy setCheckOrAnnotation(SaCheckOrAnnotationFunction checkOrAnnotation) { this.checkOrAnnotation = checkOrAnnotation; return this; } /** - * 从元素上获取注解 - *

参数 [element元素,要获取的注解类型] + * 从元素上获取注解 + * * @param getAnnotation / - * @return 对象自身 + * @return / */ - public SaStrategy setGetAnnotation(BiFunction , Annotation> getAnnotation) { + public SaStrategy setGetAnnotation(SaGetAnnotationFunction getAnnotation) { this.getAnnotation = getAnnotation; return this; } /** - * 判断一个 Method 或其所属 Class 是否包含指定注解 - *

参数 [Method, 注解] + * 判断一个 Method 或其所属 Class 是否包含指定注解 + * * @param isAnnotationPresent / - * @return 对象自身 + * @return / */ - public SaStrategy setIsAnnotationPresent(BiFunction, Boolean> isAnnotationPresent) { + public SaStrategy setIsAnnotationPresent(SaIsAnnotationPresentFunction isAnnotationPresent) { this.isAnnotationPresent = isAnnotationPresent; return this; } /** * 生成唯一式 token 的算法 - *

参数 [元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数] * * @param generateUniqueToken / - * @return 对象自身 + * @return / */ public SaStrategy setGenerateUniqueToken(SaGenerateUniqueTokenFunction generateUniqueToken) { this.generateUniqueToken = generateUniqueToken; @@ -453,12 +437,10 @@ public final class SaStrategy { /** * 创建 StpLogic 的算法 * - *

参数 [ 账号体系标识 ] - * * @param createStpLogic / - * @return 对象自身 + * @return / */ - public SaStrategy setCreateStpLogic(Function createStpLogic) { + public SaStrategy setCreateStpLogic(SaCreateStpLogicFunction createStpLogic) { this.createStpLogic = createStpLogic; return this; } diff --git a/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/SaTokenApplication.java b/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/SaTokenApplication.java index 90c20522..c667db18 100644 --- a/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/SaTokenApplication.java +++ b/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/SaTokenApplication.java @@ -1,6 +1,7 @@ package com.pj; import cn.dev33.satoken.SaManager; +import cn.dev33.satoken.strategy.SaStrategy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -16,6 +17,13 @@ public class SaTokenApplication { public static void main(String[] args) { SpringApplication.run(SaTokenApplication.class, args); System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig()); + + // SaStrategy全局单例,所有方法都用以下形式重写 + SaStrategy.instance.setCreateToken((loginId, loginType) -> { + // 自定义Token生成的算法 + return "xxxx"; + }); + } } diff --git a/sa-token-doc/fun/plugin-dev.md b/sa-token-doc/fun/plugin-dev.md index 98a4d54c..9c9d6de7 100644 --- a/sa-token-doc/fun/plugin-dev.md +++ b/sa-token-doc/fun/plugin-dev.md @@ -21,7 +21,7 @@ SaStrategy 的每一个函数都可以单独重写,以 “自定义Token生成 ``` java // 重写 Token 生成策略 -SaStrategy.me.createToken = (loginId, loginType) -> { +SaStrategy.instance.createToken = (loginId, loginType) -> { return SaFoxUtil.getRandomString(60); // 随机60位长度字符串 }; ``` diff --git a/sa-token-doc/up/many-account.md b/sa-token-doc/up/many-account.md index 12cb3022..d9ec75a0 100644 --- a/sa-token-doc/up/many-account.md +++ b/sa-token-doc/up/many-account.md @@ -92,7 +92,7 @@ public class SaTokenConfigure { @Autowired public void rewriteSaStrategy() { // 重写Sa-Token的注解处理器,增加注解合并功能 - SaStrategy.me.getAnnotation = (element, annotationClass) -> { + SaStrategy.instance.getAnnotation = (element, annotationClass) -> { return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass); }; } diff --git a/sa-token-doc/up/token-style.md b/sa-token-doc/up/token-style.md index 78f9620a..d0179ec0 100644 --- a/sa-token-doc/up/token-style.md +++ b/sa-token-doc/up/token-style.md @@ -51,7 +51,7 @@ public class SaTokenConfigure { @Autowired public void rewriteSaStrategy() { // 重写 Token 生成策略 - SaStrategy.me.createToken = (loginId, loginType) -> { + SaStrategy.instance.createToken = (loginId, loginType) -> { return SaFoxUtil.getRandomString(60); // 随机60位长度字符串 }; }