将所有策略声明抽离为单独的函数式接口

This commit is contained in:
click33 2023-06-07 14:16:52 +08:00
parent 1eb801da3c
commit 15027bbe09
15 changed files with 376 additions and 82 deletions

View File

@ -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;
/**
* 函数式接口对一个 [元素] 对象进行注解校验 注解鉴权内部实现
*
* <p> 参数element元素 </p>
* <p> 返回 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCheckElementAnnotationFunction extends Consumer<AnnotatedElement> {
}

View File

@ -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] 对象进行注解校验 注解鉴权内部实现
*
* <p> 参数Method句柄 </p>
* <p> 返回 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCheckMethodAnnotationFunction extends Consumer<Method> {
}

View File

@ -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 进行注解校验
*
* <p> 参数SaCheckOr 注解的实例 </p>
* <p> 返回 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCheckOrAnnotationFunction extends Consumer<SaCheckOr> {
}

View File

@ -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 的策略
*
* <p> 参数SessionId </p>
* <p> 返回SaSession对象 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCreateSessionFunction extends Function<String, SaSession> {
}

View File

@ -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 的算法
*
* <p> 参数账号体系标识 </p>
* <p> 返回创建好的 StpLogic 对象 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCreateStpLogicFunction extends Function<String, StpLogic> {
}

View File

@ -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 的策略
*
* <p> 参数账号 id账号类型 </p>
* <p> 返回token </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaCreateTokenFunction extends BiFunction<Object, String, String> {
}

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.dev33.satoken.fun; package cn.dev33.satoken.fun.strategy;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -21,8 +21,11 @@ import java.util.function.Supplier;
/** /**
* 生成唯一式 token 的函数式接口方便开发者进行 lambda 表达式风格调用 * 生成唯一式 token 的函数式接口方便开发者进行 lambda 表达式风格调用
* *
* <p> 参数元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数 </p>
* <p> 返回生成的token </p>
*
* @author click33 * @author click33
* @since 1.34.0 * @since 1.35.0
*/ */
@FunctionalInterface @FunctionalInterface
public interface SaGenerateUniqueTokenFunction { public interface SaGenerateUniqueTokenFunction {

View File

@ -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;
/**
* 函数式接口从元素上获取注解
*
* <p> 参数element元素要获取的注解类型 </p>
* <p> 返回注解对象 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaGetAnnotationFunction extends BiFunction<AnnotatedElement, Class<? extends Annotation> , Annotation> {
}

View File

@ -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;
/**
* 函数式接口判断集合中是否包含指定元素模糊匹配
*
* <p> 参数集合元素 </p>
* <p> 返回是否包含 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaHasElementFunction extends BiFunction<List<String>, String, Boolean> {
}

View File

@ -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 是否包含指定注解
*
* <p> 参数Method注解 </p>
* <p> 返回是否包含 </p>
*
* @author click33
* @since 1.35.0
*/
@FunctionalInterface
public interface SaIsAnnotationPresentFunction extends BiFunction<Method, Class<? extends Annotation>, Boolean> {
}

View File

@ -19,20 +19,16 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.*; import cn.dev33.satoken.annotation.*;
import cn.dev33.satoken.basic.SaBasicUtil; import cn.dev33.satoken.basic.SaBasicUtil;
import cn.dev33.satoken.exception.SaTokenException; 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.session.SaSession;
import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.util.SaFoxUtil; import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaTokenConsts; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
/** /**
@ -42,7 +38,7 @@ import java.util.function.Function;
* </p> * </p>
* <pre> * <pre>
// SaStrategy全局单例所有方法都用以下形式重写 // SaStrategy全局单例所有方法都用以下形式重写
SaStrategy.me.setCreateToken((loginId, loginType) - { SaStrategy.instance.setCreateToken((loginId, loginType) - {
// 自定义Token生成的算法 // 自定义Token生成的算法
return "xxxx"; return "xxxx";
}); });
@ -65,10 +61,9 @@ public final class SaStrategy {
// ----------------------- 所有策略 // ----------------------- 所有策略
/** /**
* 创建 Token 的策略 * 创建 Token 的策略
* <p> 参数 [ 账号id, 账号类型 ]
*/ */
public BiFunction<Object, String, String> createToken = (loginId, loginType) -> { public SaCreateTokenFunction createToken = (loginId, loginType) -> {
// 根据配置的tokenStyle生成不同风格的token // 根据配置的tokenStyle生成不同风格的token
String tokenStyle = SaManager.getConfig().getTokenStyle(); String tokenStyle = SaManager.getConfig().getTokenStyle();
@ -106,18 +101,16 @@ public final class SaStrategy {
}; };
/** /**
* 创建 Session 的策略 * 创建 Session 的策略
* <p> 参数 [SessionId]
*/ */
public Function<String, SaSession> createSession = (sessionId) -> { public SaCreateSessionFunction createSession = (sessionId) -> {
return new SaSession(sessionId); return new SaSession(sessionId);
}; };
/** /**
* 判断集合中是否包含指定元素模糊匹配 * 判断集合中是否包含指定元素模糊匹配
* <p> 参数 [集合, 元素]
*/ */
public BiFunction<List<String>, String, Boolean> hasElement = (list, element) -> { public SaHasElementFunction hasElement = (list, element) -> {
// 空集合直接返回false // 空集合直接返回false
if(list == null || list.size() == 0) { if(list == null || list.size() == 0) {
@ -141,10 +134,9 @@ public final class SaStrategy {
}; };
/** /**
* 对一个 [Method] 对象进行注解校验 注解鉴权内部实现 * 对一个 [Method] 对象进行注解校验 注解鉴权内部实现
* <p> 参数 [Method句柄]
*/ */
public Consumer<Method> checkMethodAnnotation = (method) -> { public SaCheckMethodAnnotationFunction checkMethodAnnotation = (method) -> {
// 先校验 Method 所属 Class 上的注解 // 先校验 Method 所属 Class 上的注解
instance.checkElementAnnotation.accept(method.getDeclaringClass()); instance.checkElementAnnotation.accept(method.getDeclaringClass());
@ -154,49 +146,48 @@ public final class SaStrategy {
}; };
/** /**
* 对一个 [元素] 对象进行注解校验 注解鉴权内部实现 * 对一个 [元素] 对象进行注解校验 注解鉴权内部实现
* <p> 参数 [element元素]
*/ */
public Consumer<AnnotatedElement> checkElementAnnotation = (target) -> { public SaCheckElementAnnotationFunction checkElementAnnotation = (element) -> {
// 校验 @SaCheckLogin 注解 // 校验 @SaCheckLogin 注解
SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.instance.getAnnotation.apply(target, SaCheckLogin.class); SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.instance.getAnnotation.apply(element, SaCheckLogin.class);
if(checkLogin != null) { if(checkLogin != null) {
SaManager.getStpLogic(checkLogin.type(), false).checkByAnnotation(checkLogin); SaManager.getStpLogic(checkLogin.type(), false).checkByAnnotation(checkLogin);
} }
// 校验 @SaCheckRole 注解 // 校验 @SaCheckRole 注解
SaCheckRole checkRole = (SaCheckRole) SaStrategy.instance.getAnnotation.apply(target, SaCheckRole.class); SaCheckRole checkRole = (SaCheckRole) SaStrategy.instance.getAnnotation.apply(element, SaCheckRole.class);
if(checkRole != null) { if(checkRole != null) {
SaManager.getStpLogic(checkRole.type(), false).checkByAnnotation(checkRole); SaManager.getStpLogic(checkRole.type(), false).checkByAnnotation(checkRole);
} }
// 校验 @SaCheckPermission 注解 // 校验 @SaCheckPermission 注解
SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.instance.getAnnotation.apply(target, SaCheckPermission.class); SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.instance.getAnnotation.apply(element, SaCheckPermission.class);
if(checkPermission != null) { if(checkPermission != null) {
SaManager.getStpLogic(checkPermission.type(), false).checkByAnnotation(checkPermission); SaManager.getStpLogic(checkPermission.type(), false).checkByAnnotation(checkPermission);
} }
// 校验 @SaCheckSafe 注解 // 校验 @SaCheckSafe 注解
SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.instance.getAnnotation.apply(target, SaCheckSafe.class); SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.instance.getAnnotation.apply(element, SaCheckSafe.class);
if(checkSafe != null) { if(checkSafe != null) {
SaManager.getStpLogic(checkSafe.type(), false).checkByAnnotation(checkSafe); SaManager.getStpLogic(checkSafe.type(), false).checkByAnnotation(checkSafe);
} }
// 校验 @SaCheckDisable 注解 // 校验 @SaCheckDisable 注解
SaCheckDisable checkDisable = (SaCheckDisable) SaStrategy.instance.getAnnotation.apply(target, SaCheckDisable.class); SaCheckDisable checkDisable = (SaCheckDisable) SaStrategy.instance.getAnnotation.apply(element, SaCheckDisable.class);
if(checkDisable != null) { if(checkDisable != null) {
SaManager.getStpLogic(checkDisable.type(), false).checkByAnnotation(checkDisable); SaManager.getStpLogic(checkDisable.type(), false).checkByAnnotation(checkDisable);
} }
// 校验 @SaCheckBasic 注解 // 校验 @SaCheckBasic 注解
SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.instance.getAnnotation.apply(target, SaCheckBasic.class); SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.instance.getAnnotation.apply(element, SaCheckBasic.class);
if(checkBasic != null) { if(checkBasic != null) {
SaBasicUtil.check(checkBasic.realm(), checkBasic.account()); SaBasicUtil.check(checkBasic.realm(), checkBasic.account());
} }
// 校验 @SaCheckOr 注解 // 校验 @SaCheckOr 注解
SaCheckOr checkOr = (SaCheckOr) SaStrategy.instance.getAnnotation.apply(target, SaCheckOr.class); SaCheckOr checkOr = (SaCheckOr) SaStrategy.instance.getAnnotation.apply(element, SaCheckOr.class);
if(checkOr != null) { if(checkOr != null) {
SaStrategy.instance.checkOrAnnotation.accept(checkOr); SaStrategy.instance.checkOrAnnotation.accept(checkOr);
} }
@ -204,9 +195,8 @@ public final class SaStrategy {
/** /**
* 对一个 @SaCheckOr 进行注解校验 * 对一个 @SaCheckOr 进行注解校验
* <p> 参数 [SaCheckOr 注解的实例]
*/ */
public Consumer<SaCheckOr> checkOrAnnotation = (at) -> { public SaCheckOrAnnotationFunction checkOrAnnotation = (at) -> {
// 记录校验过程中所有的异常 // 记录校验过程中所有的异常
List<SaTokenException> errorList = new ArrayList<>(); List<SaTokenException> errorList = new ArrayList<>();
@ -290,26 +280,23 @@ public final class SaStrategy {
}; };
/** /**
* 从元素上获取注解 * 从元素上获取注解
* <p> 参数 [element元素要获取的注解类型]
*/ */
public BiFunction<AnnotatedElement, Class<? extends Annotation> , Annotation> getAnnotation = (element, annotationClass)->{ public SaGetAnnotationFunction getAnnotation = (element, annotationClass)->{
// 默认使用jdk的注解处理器 // 默认使用jdk的注解处理器
return element.getAnnotation(annotationClass); return element.getAnnotation(annotationClass);
}; };
/** /**
* 判断一个 Method 或其所属 Class 是否包含指定注解 * 判断一个 Method 或其所属 Class 是否包含指定注解
* <p> 参数 [Method, 注解]
*/ */
public BiFunction<Method, Class<? extends Annotation>, Boolean> isAnnotationPresent = (method, annotationClass) -> { public SaIsAnnotationPresentFunction isAnnotationPresent = (method, annotationClass) -> {
return instance.getAnnotation.apply(method, annotationClass) != null || return instance.getAnnotation.apply(method, annotationClass) != null ||
instance.getAnnotation.apply(method.getDeclaringClass(), annotationClass) != null; instance.getAnnotation.apply(method.getDeclaringClass(), annotationClass) != null;
}; };
/** /**
* 生成唯一式 token 的算法 * 生成唯一式 token 的算法
* <p> 参数 [元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数]
*/ */
public SaGenerateUniqueTokenFunction generateUniqueToken = (elementName, maxTryTimes, createTokenFunction, checkTokenFunction) -> { public SaGenerateUniqueTokenFunction generateUniqueToken = (elementName, maxTryTimes, createTokenFunction, checkTokenFunction) -> {
@ -339,10 +326,8 @@ public final class SaStrategy {
/** /**
* 创建 StpLogic 的算法 * 创建 StpLogic 的算法
*
* <p> 参数 [ 账号体系标识 ]
*/ */
public Function<String, StpLogic> createStpLogic = (loginType) -> { public SaCreateStpLogicFunction createStpLogic = (loginType) -> {
return new StpLogic(loginType); return new StpLogic(loginType);
}; };
@ -350,56 +335,56 @@ public final class SaStrategy {
// ----------------------- 重写策略 set连缀风格 // ----------------------- 重写策略 set连缀风格
/** /**
* 重写创建 Token 的策略 * 重写创建 Token 的策略
* <p> 参数 [账号id, 账号类型] *
* @param createToken / * @param createToken /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCreateToken(BiFunction<Object, String, String> createToken) { public SaStrategy setCreateToken(SaCreateTokenFunction createToken) {
this.createToken = createToken; this.createToken = createToken;
return this; return this;
} }
/** /**
* 重写创建 Session 的策略 * 重写创建 Session 的策略
* <p> 参数 [SessionId] *
* @param createSession / * @param createSession /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCreateSession(Function<String, SaSession> createSession) { public SaStrategy setCreateSession(SaCreateSessionFunction createSession) {
this.createSession = createSession; this.createSession = createSession;
return this; return this;
} }
/** /**
* 判断集合中是否包含指定元素模糊匹配 * 判断集合中是否包含指定元素模糊匹配
* <p> 参数 [集合, 元素] *
* @param hasElement / * @param hasElement /
* @return 对象自身 * @return /
*/ */
public SaStrategy setHasElement(BiFunction<List<String>, String, Boolean> hasElement) { public SaStrategy setHasElement(SaHasElementFunction hasElement) {
this.hasElement = hasElement; this.hasElement = hasElement;
return this; return this;
} }
/** /**
* 对一个 [Method] 对象进行注解校验 注解鉴权内部实现 * 对一个 [Method] 对象进行注解校验 注解鉴权内部实现
* <p> 参数 [Method句柄] *
* @param checkMethodAnnotation / * @param checkMethodAnnotation /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCheckMethodAnnotation(Consumer<Method> checkMethodAnnotation) { public SaStrategy setCheckMethodAnnotation(SaCheckMethodAnnotationFunction checkMethodAnnotation) {
this.checkMethodAnnotation = checkMethodAnnotation; this.checkMethodAnnotation = checkMethodAnnotation;
return this; return this;
} }
/** /**
* 对一个 [元素] 对象进行注解校验 注解鉴权内部实现 * 对一个 [元素] 对象进行注解校验 注解鉴权内部实现
* <p> 参数 [element元素] *
* @param checkElementAnnotation / * @param checkElementAnnotation /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCheckElementAnnotation(Consumer<AnnotatedElement> checkElementAnnotation) { public SaStrategy setCheckElementAnnotation(SaCheckElementAnnotationFunction checkElementAnnotation) {
this.checkElementAnnotation = checkElementAnnotation; this.checkElementAnnotation = checkElementAnnotation;
return this; return this;
} }
@ -409,41 +394,40 @@ public final class SaStrategy {
* <p> 参数 [SaCheckOr 注解的实例] * <p> 参数 [SaCheckOr 注解的实例]
* *
* @param checkOrAnnotation / * @param checkOrAnnotation /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCheckOrAnnotation(Consumer<SaCheckOr> checkOrAnnotation) { public SaStrategy setCheckOrAnnotation(SaCheckOrAnnotationFunction checkOrAnnotation) {
this.checkOrAnnotation = checkOrAnnotation; this.checkOrAnnotation = checkOrAnnotation;
return this; return this;
} }
/** /**
* 从元素上获取注解 * 从元素上获取注解
* <p> 参数 [element元素要获取的注解类型] *
* @param getAnnotation / * @param getAnnotation /
* @return 对象自身 * @return /
*/ */
public SaStrategy setGetAnnotation(BiFunction<AnnotatedElement, Class<? extends Annotation> , Annotation> getAnnotation) { public SaStrategy setGetAnnotation(SaGetAnnotationFunction getAnnotation) {
this.getAnnotation = getAnnotation; this.getAnnotation = getAnnotation;
return this; return this;
} }
/** /**
* 判断一个 Method 或其所属 Class 是否包含指定注解 * 判断一个 Method 或其所属 Class 是否包含指定注解
* <p> 参数 [Method, 注解] *
* @param isAnnotationPresent / * @param isAnnotationPresent /
* @return 对象自身 * @return /
*/ */
public SaStrategy setIsAnnotationPresent(BiFunction<Method, Class<? extends Annotation>, Boolean> isAnnotationPresent) { public SaStrategy setIsAnnotationPresent(SaIsAnnotationPresentFunction isAnnotationPresent) {
this.isAnnotationPresent = isAnnotationPresent; this.isAnnotationPresent = isAnnotationPresent;
return this; return this;
} }
/** /**
* 生成唯一式 token 的算法 * 生成唯一式 token 的算法
* <p> 参数 [元素名称, 最大尝试次数, 创建 token 函数, 检查 token 函数]
* *
* @param generateUniqueToken / * @param generateUniqueToken /
* @return 对象自身 * @return /
*/ */
public SaStrategy setGenerateUniqueToken(SaGenerateUniqueTokenFunction generateUniqueToken) { public SaStrategy setGenerateUniqueToken(SaGenerateUniqueTokenFunction generateUniqueToken) {
this.generateUniqueToken = generateUniqueToken; this.generateUniqueToken = generateUniqueToken;
@ -453,12 +437,10 @@ public final class SaStrategy {
/** /**
* 创建 StpLogic 的算法 * 创建 StpLogic 的算法
* *
* <p> 参数 [ 账号体系标识 ]
*
* @param createStpLogic / * @param createStpLogic /
* @return 对象自身 * @return /
*/ */
public SaStrategy setCreateStpLogic(Function<String, StpLogic> createStpLogic) { public SaStrategy setCreateStpLogic(SaCreateStpLogicFunction createStpLogic) {
this.createStpLogic = createStpLogic; this.createStpLogic = createStpLogic;
return this; return this;
} }

View File

@ -1,6 +1,7 @@
package com.pj; package com.pj;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.strategy.SaStrategy;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -16,6 +17,13 @@ public class SaTokenApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SaTokenApplication.class, args); SpringApplication.run(SaTokenApplication.class, args);
System.out.println("\n启动成功Sa-Token配置如下" + SaManager.getConfig()); System.out.println("\n启动成功Sa-Token配置如下" + SaManager.getConfig());
// SaStrategy全局单例所有方法都用以下形式重写
SaStrategy.instance.setCreateToken((loginId, loginType) -> {
// 自定义Token生成的算法
return "xxxx";
});
} }
} }

View File

@ -21,7 +21,7 @@ SaStrategy 的每一个函数都可以单独重写,以 “自定义Token生成
``` java ``` java
// 重写 Token 生成策略 // 重写 Token 生成策略
SaStrategy.me.createToken = (loginId, loginType) -> { SaStrategy.instance.createToken = (loginId, loginType) -> {
return SaFoxUtil.getRandomString(60); // 随机60位长度字符串 return SaFoxUtil.getRandomString(60); // 随机60位长度字符串
}; };
``` ```

View File

@ -92,7 +92,7 @@ public class SaTokenConfigure {
@Autowired @Autowired
public void rewriteSaStrategy() { public void rewriteSaStrategy() {
// 重写Sa-Token的注解处理器增加注解合并功能 // 重写Sa-Token的注解处理器增加注解合并功能
SaStrategy.me.getAnnotation = (element, annotationClass) -> { SaStrategy.instance.getAnnotation = (element, annotationClass) -> {
return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass); return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
}; };
} }

View File

@ -51,7 +51,7 @@ public class SaTokenConfigure {
@Autowired @Autowired
public void rewriteSaStrategy() { public void rewriteSaStrategy() {
// 重写 Token 生成策略 // 重写 Token 生成策略
SaStrategy.me.createToken = (loginId, loginType) -> { SaStrategy.instance.createToken = (loginId, loginType) -> {
return SaFoxUtil.getRandomString(60); // 随机60位长度字符串 return SaFoxUtil.getRandomString(60); // 随机60位长度字符串
}; };
} }