Context info添加两个默认的实现

This commit is contained in:
TanJ 2023-03-30 14:47:29 +08:00
parent fed63c5607
commit 1d01a34cdf
6 changed files with 76 additions and 20 deletions

View File

@ -15,5 +15,9 @@ import cn.axzo.framework.auth.domain.contextvalidate.ContextInfoHandler;
public @interface PreBuildContext { public @interface PreBuildContext {
Class<? extends ContextInfo> value() default ContextInfo.class; Class<? extends ContextInfo> value() default ContextInfo.class;
/**
* 针对ContextInfo进行一些定制化处理会按传入的顺序执行
* @return
*/
Class<? extends ContextInfoHandler>[] filter() default {}; Class<? extends ContextInfoHandler>[] filter() default {};
} }

View File

@ -8,5 +8,16 @@ import cn.axzo.framework.auth.domain.ContextInfo;
*/ */
public interface ContextInfoHandler { public interface ContextInfoHandler {
void handler(ContextInfo contextInfo); /**
*
* @param contextInfo
* @return
* <B>
* ***一般来说返回null就行 若返回了非null.则会阻断业务代码运行直接返回对应的参数给到前端 ***
* <br>
* ***谨慎返回***
*
* </B>
*/
Object handler(ContextInfo contextInfo);
} }

View File

@ -0,0 +1,31 @@
package cn.axzo.framework.auth.domain.contextvalidate;
import cn.axzo.framework.auth.domain.ContextInfo;
import cn.azxo.framework.common.model.CommonResponse;
import java.lang.reflect.InvocationTargetException;
/**
* 某些接口必须要这两个参数校验这两个参数只要有一个字段为0或者null,则不执行业务逻辑
* @author tanjie@axzo.cn
* @date 2023/3/29 11:33
*/
public class NonWorkspace implements ContextInfoHandler{
@Override
public Object handler(ContextInfo contextInfo) {
if (null != contextInfo
&& (isZero(contextInfo.getOuId()) || isZero(contextInfo.getWorkspaceId()) )) {
return CommonResponse.success();
}
return null;
}
private boolean isZero(Long number) {
if(number == null)
return true;
if(number == 0L)
return true;
return false;
}
}

View File

@ -0,0 +1,25 @@
package cn.axzo.framework.auth.domain.contextvalidate;
import cn.axzo.framework.auth.AuthException;
import cn.axzo.framework.auth.domain.ContextInfo;
import cn.axzo.framework.auth.domain.UserInfo;
import cn.axzo.framework.auth.enums.VerifyStatusEnum;
import cn.hutool.core.lang.Assert;
/**
* 实名校验
* @author tanjie@axzo.cn
* @date 2023/3/29 11:12
*/
public class ValidateVerify implements ContextInfoHandler{
@Override
public Object handler(ContextInfo contextInfo) {
Assert.isTrue((null != contextInfo && null != contextInfo.getUserInfo() )&& VerifyStatusEnum.VERIFY_SUCCESS.equals(contextInfo.getUserInfo().getVerifyStatus())
, () -> new AuthException("该功能需要实名认证之后才可使用")
);
return null;
}
}

View File

@ -1,18 +0,0 @@
package cn.axzo.framework.auth.domain.contextvalidate;
import cn.axzo.framework.auth.AuthException;
import cn.axzo.framework.auth.domain.ContextInfo;
/**
* @author tanjie@axzo.cn
* @date 2023/3/29 11:12
*/
public class VerifyContext implements ContextInfoHandler{
@Override
public void handler(ContextInfo contextInfo) {
Integer verifiedStatus = contextInfo.getUserInfo().getVerifiedStatus();
if (2 != verifiedStatus) {
throw new AuthException("该功能需要实名认证之后才可使用");
}
}
}

View File

@ -127,7 +127,10 @@ public class ContextInfoBuilderAspect {
contextInfo.buildCustomInfo(httpRequest, contextInfo.getUserInfoMap()); contextInfo.buildCustomInfo(httpRequest, contextInfo.getUserInfoMap());
//针对contextInfo拦截或修改参数等 //针对contextInfo拦截或修改参数等
for (Class<? extends ContextInfoHandler> filter : preBuildContext.filter()) { for (Class<? extends ContextInfoHandler> filter : preBuildContext.filter()) {
filter.newInstance().handler(contextInfo); Object handler = filter.newInstance().handler(contextInfo);
if (null != handler) {
return handler;
}
} }
// 把ContextInfo放到ThreadLocal中 // 把ContextInfo放到ThreadLocal中
ContextInfoHolder.set(contextInfo); ContextInfoHolder.set(contextInfo);