mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-09 08:20:31 +08:00
dee355b4a7
- artifacts/decompiled 树与相关源码变更 - maven-cw-elevator-application 业务 docs 与 package-info - scripts 下 formatter 校验与辅助脚本 - 其他子工程/接口与发布线一并纳入版本控制 Made-with: Cursor Former-commit-id: e102e8cab64e575bcd23c9a66a598aa1892bb492
124 lines
3.0 KiB
Java
124 lines
3.0 KiB
Java
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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();
|
|
}
|
|
}
|
|
|
|
|