mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-10 00:40:30 +08:00
Initial commit: five Maven reactors and docs only
Track maven-cloudwalk-cloud, maven-cw-elevator-application, maven-intelligent-cwoscomponent, maven-ninca-crk, maven-ninca-qk-alarm, and docs/. Other workspace paths ignored via .gitignore. Made-with: Cursor
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
package cn.cloudwalk.service.aop;
|
||||
|
||||
import cn.cloudwalk.cloud.annotation.CloudwalkParamsValidate;
|
||||
import cn.cloudwalk.cloud.result.CloudwalkResult;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.groups.Default;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 对标注了 {@link cn.cloudwalk.cloud.annotation.CloudwalkParamsValidate} 的入口进行 Bean Validation 校验。
|
||||
*/
|
||||
@Aspect
|
||||
@Order(-100)
|
||||
@Component
|
||||
public class CloudwalkParamsValidateAspect {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
public String getMessage(String code) {
|
||||
return this.messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
|
||||
}
|
||||
|
||||
@Pointcut("@annotation(cn.cloudwalk.cloud.annotation.CloudwalkParamsValidate)")
|
||||
public void validatePointcat() {}
|
||||
|
||||
@Around("validatePointcat()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
|
||||
Method targetMethod =
|
||||
joinPoint.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
|
||||
this.logger.debug("正在进行对类{}中方法{}进行参数检查开始", joinPoint.getTarget().getClass().getName(), targetMethod.getName());
|
||||
CloudwalkParamsValidate paramsValidate =
|
||||
targetMethod.<CloudwalkParamsValidate>getAnnotation(CloudwalkParamsValidate.class);
|
||||
if (paramsValidate == null) {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
int[] needCheckParamIndexs = paramsValidate.argsIndexs();
|
||||
if (needCheckParamIndexs == null) {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
Class<?>[] groupsClazz = paramsValidate.groups();
|
||||
if (groupsClazz == null) {
|
||||
groupsClazz = new Class[] {Default.class};
|
||||
}
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (int index : needCheckParamIndexs) {
|
||||
Object arg = args[index];
|
||||
Set<ConstraintViolation<Object>> constraintViolationSet = this.validator.validate(arg, groupsClazz);
|
||||
if (constraintViolationSet != null && constraintViolationSet.size() > 0) {
|
||||
Iterator<ConstraintViolation<Object>> iterator = constraintViolationSet.iterator();
|
||||
if (iterator.hasNext()) {
|
||||
ConstraintViolation<Object> constraintViolation = iterator.next();
|
||||
String code = constraintViolation.getMessage();
|
||||
return CloudwalkResult.fail(code, getMessage(code));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.logger.debug("正在进行对类{}中方法{}进行参数检查通过", joinPoint.getTarget().getClass().getName(), targetMethod.getName());
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user