Commit 92bab3a6 authored by 周健威's avatar 周健威

Merge remote-tracking branch 'origin/base-modify' into base-modify

parents d31ad520 56b174e3
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
</properties> </properties>
<dependencies> <dependencies>
<!--<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency>--> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
...@@ -53,22 +53,22 @@ ...@@ -53,22 +53,22 @@
<artifactId>mapper</artifactId> <artifactId>mapper</artifactId>
<version>3.4.0</version> <version>3.4.0</version>
</dependency> </dependency>
<dependency> <!-- <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId> <artifactId>spring-beans</artifactId>
<version>5.0.4.RELEASE</version> <version>5.0.4.RELEASE</version>
</dependency> </dependency>-->
<dependency> <dependency>
<groupId>com.github.pagehelper</groupId> <groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId> <artifactId>pagehelper</artifactId>
<version>5.0.3</version> <version>5.0.3</version>
</dependency> </dependency>
<dependency> <!-- <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version> <version>5.0.4.RELEASE</version>
</dependency> </dependency>-->
<!-- fastjson.jar --> <!-- fastjson.jar -->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
......
package com.github.wxiaoqi.security.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author libin
* @version 1.0
* @description 复杂类型校验
* @data 2019/6/13 13:42
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface BeanValid {
Class<?>[] value() default {};
}
package com.github.wxiaoqi.security.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author libin
* @version 1.0
* @description 简单参数类型校验
* @data 2019/6/13 13:42
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface SimpleValid {
}
...@@ -9,9 +9,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; ...@@ -9,9 +9,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice("com.xxfc.platform") @RestControllerAdvice("com.xxfc.platform")
public class PlatformExceptionHandler { public class PlatformExceptionHandler {
@ExceptionHandler(BaseException.class) @ExceptionHandler(value = {BaseException.class,Exception.class})
public BaseResponse baseExceptionHandler(BaseException e) { public BaseResponse baseExceptionHandler(Exception e) {
return new BaseResponse(e.getStatus(), e.getMessage()); if (e instanceof BaseException){
BaseException be = (BaseException) e;
return new BaseResponse(be.getStatus(), be.getMessage());
}
return new BaseResponse(400,e.getMessage());
} }
} }
package com.github.wxiaoqi.security.common.support.aop;
import cn.hutool.core.util.ArrayUtil;
import com.github.wxiaoqi.security.common.annotation.BeanValid;
import com.github.wxiaoqi.security.common.annotation.SimpleValid;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import lombok.Data;
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 javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Objects;
import java.util.Set;
/**
* @author libin
* @version 1.0
* @description 切面验证
* @data 2019/6/13 13:46
*/
@Aspect
@Data
public class ValidParamAop {
/**
* 复合类型验证器
*/
private Validator validator;
/**
* 简单类型验证器
*/
private ExecutableValidator executableValidator;
@Pointcut("execution(* com.xxfc.platform..rest..*(..))")
public void validatorExpress(){}
@Around("validatorExpress()")
public Object validatorAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
//获取方法的所有参数
Object[] args = proceedingJoinPoint.getArgs();
if (ArrayUtil.isEmpty(args)){
//没有参数,不用验证
return proceedingJoinPoint.proceed();
}
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method currentMethod = methodSignature.getMethod();
Parameter[] parameters = currentMethod.getParameters();
boolean hashSimpleValid = false;
int argLength = args.length;
for (int i =0;i<argLength;i++){
BeanValid beanValidAnnotation = parameters[i].getDeclaredAnnotation(BeanValid.class);
if (Objects.nonNull(beanValidAnnotation)){
if (args[i].getClass().isArray()){
Object[] arrayArg =(Object[])args[i];
for (Object item:arrayArg){
Set<ConstraintViolation<Object>> validateResult = this.validator.validate(item, beanValidAnnotation.value());
if (!validateResult.isEmpty()){
String message = validateResult.iterator().next().getMessage();
return ObjectRestResponse.createFailedResult(400,message);
}
}
}else {
Set<ConstraintViolation<Object>> validateResult = this.validator.validate(args[i], beanValidAnnotation.value());
if (!validateResult.isEmpty()){
String message = validateResult.iterator().next().getMessage();
return ObjectRestResponse.createFailedResult(400,message);
}
}
continue;
}
if (!hashSimpleValid){
SimpleValid simpleValidAnnotation = parameters[i].getDeclaredAnnotation(SimpleValid.class);
if (Objects.nonNull(simpleValidAnnotation)){
hashSimpleValid = true;
}
}
}
if (hashSimpleValid){
Object target = proceedingJoinPoint.getTarget();
Set<ConstraintViolation<Object>> validResult = this.executableValidator.validateParameters(target, currentMethod, args);
if(!validResult.isEmpty()){
String message = validResult.iterator().next().getMessage();
return ObjectRestResponse.createFailedResult(400,message);
}
}
return proceedingJoinPoint.proceed();
}
}
package com.github.wxiaoqi.security.common.support.config;
import com.github.wxiaoqi.security.common.support.aop.ValidParamAop;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
/**
* @author libin
* @version 1.0
* @description 验证器validator
* @data 2019/6/13 13:48
*/
@Configuration
public class ValidatorConfig {
/**
* 复合类型所用的验证器
* @return Validator
*/
@Bean
public Validator validator(){
// .failFast( true ) 为设置快速错误模式,默认为false
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
.configure()
.failFast( true )
.buildValidatorFactory();
return validatorFactory.getValidator();
}
/**
* 简单类型所用的验证器
* @return ExecutableValidator
*/
@Bean
public ExecutableValidator executableValidator() {
return validator().forExecutables();
}
/**
* 装配验证器切面
* @param validator Bean验证器
* @param executableValidator 简单类型验证器
* @return ValidParamAop
*/
@Bean
public ValidParamAop validAop(Validator validator, ExecutableValidator executableValidator) {
ValidParamAop validParamAop = new ValidParamAop();
validParamAop.setValidator(validator);
validParamAop.setExecutableValidator(executableValidator);
return validParamAop;
}
}
...@@ -101,6 +101,16 @@ ...@@ -101,6 +101,16 @@
<groupId>com.github.wxiaoqi</groupId> <groupId>com.github.wxiaoqi</groupId>
<artifactId>ace-common</artifactId> <artifactId>ace-common</artifactId>
<version>2.0-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
......
...@@ -30,10 +30,10 @@ public class TourGoodVerification implements Serializable { ...@@ -30,10 +30,10 @@ public class TourGoodVerification implements Serializable {
private Integer id; private Integer id;
/** /**
* 日期规格 * 商品规格id
*/ */
@Column(name = "spe_id") @Column(name = "spe_id")
@ApiModelProperty(value = "日期规格id") @ApiModelProperty(value = "商品规格id")
private Integer speId; private Integer speId;
/** /**
......
...@@ -28,4 +28,6 @@ public class TourGoodVerificationBiz extends BaseBiz<TourGoodVerificationMapper, ...@@ -28,4 +28,6 @@ public class TourGoodVerificationBiz extends BaseBiz<TourGoodVerificationMapper,
mapper.insertTourGoodVerification(tourGoodVerification); mapper.insertTourGoodVerification(tourGoodVerification);
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment