6 changed files with 93 additions and 3 deletions
@ -0,0 +1,24 @@ |
|||||
|
package com.ruoyi.common.utils.bean; |
||||
|
|
||||
|
import java.util.Set; |
||||
|
import javax.validation.ConstraintViolation; |
||||
|
import javax.validation.ConstraintViolationException; |
||||
|
import javax.validation.Validator; |
||||
|
|
||||
|
/** |
||||
|
* bean对象属性验证 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
public class BeanValidators |
||||
|
{ |
||||
|
public static void validateWithException(Validator validator, Object object, Class<?>... groups) |
||||
|
throws ConstraintViolationException |
||||
|
{ |
||||
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); |
||||
|
if (!constraintViolations.isEmpty()) |
||||
|
{ |
||||
|
throw new ConstraintViolationException(constraintViolations); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package com.ruoyi.common.xss; |
||||
|
|
||||
|
import javax.validation.Constraint; |
||||
|
import javax.validation.Payload; |
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
|
||||
|
/** |
||||
|
* 自定义xss校验注解 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
@Target(value = { ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER }) |
||||
|
@Constraint(validatedBy = { XssValidator.class }) |
||||
|
public @interface Xss |
||||
|
{ |
||||
|
String message() |
||||
|
|
||||
|
default "不允许任何脚本运行"; |
||||
|
|
||||
|
Class<?>[] groups() default {}; |
||||
|
|
||||
|
Class<? extends Payload>[] payload() default {}; |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package com.ruoyi.common.xss; |
||||
|
|
||||
|
import javax.validation.ConstraintValidator; |
||||
|
import javax.validation.ConstraintValidatorContext; |
||||
|
import java.util.regex.Matcher; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
/** |
||||
|
* 自定义xss校验注解实现 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
public class XssValidator implements ConstraintValidator<Xss, String> |
||||
|
{ |
||||
|
private final String HTML_PATTERN = "<(\\S*?)[^>]*>.*?|<.*? />"; |
||||
|
|
||||
|
@Override |
||||
|
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) |
||||
|
{ |
||||
|
return !containsHtml(value); |
||||
|
} |
||||
|
|
||||
|
public boolean containsHtml(String value) |
||||
|
{ |
||||
|
Pattern pattern = Pattern.compile(HTML_PATTERN); |
||||
|
Matcher matcher = pattern.matcher(value); |
||||
|
return matcher.matches(); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue