正在显示
6 个修改的文件
包含
93 行增加
和
3 行删除
| @@ -2,9 +2,7 @@ package com.ruoyi.common.core.domain.entity; | @@ -2,9 +2,7 @@ package com.ruoyi.common.core.domain.entity; | ||
| 2 | 2 | ||
| 3 | import java.util.Date; | 3 | import java.util.Date; |
| 4 | import java.util.List; | 4 | import java.util.List; |
| 5 | -import javax.validation.constraints.Email; | ||
| 6 | -import javax.validation.constraints.NotBlank; | ||
| 7 | -import javax.validation.constraints.Size; | 5 | +import javax.validation.constraints.*; |
| 8 | import org.apache.commons.lang3.builder.ToStringBuilder; | 6 | import org.apache.commons.lang3.builder.ToStringBuilder; |
| 9 | import org.apache.commons.lang3.builder.ToStringStyle; | 7 | import org.apache.commons.lang3.builder.ToStringStyle; |
| 10 | import com.fasterxml.jackson.annotation.JsonIgnore; | 8 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| @@ -14,6 +12,7 @@ import com.ruoyi.common.annotation.Excel.ColumnType; | @@ -14,6 +12,7 @@ import com.ruoyi.common.annotation.Excel.ColumnType; | ||
| 14 | import com.ruoyi.common.annotation.Excel.Type; | 12 | import com.ruoyi.common.annotation.Excel.Type; |
| 15 | import com.ruoyi.common.annotation.Excels; | 13 | import com.ruoyi.common.annotation.Excels; |
| 16 | import com.ruoyi.common.core.domain.BaseEntity; | 14 | import com.ruoyi.common.core.domain.BaseEntity; |
| 15 | +import com.ruoyi.common.xss.Xss; | ||
| 17 | 16 | ||
| 18 | /** | 17 | /** |
| 19 | * 用户对象 sys_user | 18 | * 用户对象 sys_user |
| @@ -135,6 +134,7 @@ public class SysUser extends BaseEntity | @@ -135,6 +134,7 @@ public class SysUser extends BaseEntity | ||
| 135 | this.deptId = deptId; | 134 | this.deptId = deptId; |
| 136 | } | 135 | } |
| 137 | 136 | ||
| 137 | + @Xss(message = "用户昵称不能包含脚本字符") | ||
| 138 | @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") | 138 | @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") |
| 139 | public String getNickName() | 139 | public String getNickName() |
| 140 | { | 140 | { |
| @@ -146,6 +146,7 @@ public class SysUser extends BaseEntity | @@ -146,6 +146,7 @@ public class SysUser extends BaseEntity | ||
| 146 | this.nickName = nickName; | 146 | this.nickName = nickName; |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | + @Xss(message = "用户账号不能包含脚本字符") | ||
| 149 | @NotBlank(message = "用户账号不能为空") | 150 | @NotBlank(message = "用户账号不能为空") |
| 150 | @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符") | 151 | @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符") |
| 151 | public String getUserName() | 152 | public String getUserName() |
| 1 | +package com.ruoyi.common.utils.bean; | ||
| 2 | + | ||
| 3 | +import java.util.Set; | ||
| 4 | +import javax.validation.ConstraintViolation; | ||
| 5 | +import javax.validation.ConstraintViolationException; | ||
| 6 | +import javax.validation.Validator; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * bean对象属性验证 | ||
| 10 | + * | ||
| 11 | + * @author ruoyi | ||
| 12 | + */ | ||
| 13 | +public class BeanValidators | ||
| 14 | +{ | ||
| 15 | + public static void validateWithException(Validator validator, Object object, Class<?>... groups) | ||
| 16 | + throws ConstraintViolationException | ||
| 17 | + { | ||
| 18 | + Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); | ||
| 19 | + if (!constraintViolations.isEmpty()) | ||
| 20 | + { | ||
| 21 | + throw new ConstraintViolationException(constraintViolations); | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | +} |
| 1 | +package com.ruoyi.common.xss; | ||
| 2 | + | ||
| 3 | +import javax.validation.Constraint; | ||
| 4 | +import javax.validation.Payload; | ||
| 5 | +import java.lang.annotation.ElementType; | ||
| 6 | +import java.lang.annotation.Retention; | ||
| 7 | +import java.lang.annotation.RetentionPolicy; | ||
| 8 | +import java.lang.annotation.Target; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 自定义xss校验注解 | ||
| 12 | + * | ||
| 13 | + * @author ruoyi | ||
| 14 | + */ | ||
| 15 | +@Retention(RetentionPolicy.RUNTIME) | ||
| 16 | +@Target(value = { ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER }) | ||
| 17 | +@Constraint(validatedBy = { XssValidator.class }) | ||
| 18 | +public @interface Xss | ||
| 19 | +{ | ||
| 20 | + String message() | ||
| 21 | + | ||
| 22 | + default "不允许任何脚本运行"; | ||
| 23 | + | ||
| 24 | + Class<?>[] groups() default {}; | ||
| 25 | + | ||
| 26 | + Class<? extends Payload>[] payload() default {}; | ||
| 27 | +} |
| 1 | +package com.ruoyi.common.xss; | ||
| 2 | + | ||
| 3 | +import javax.validation.ConstraintValidator; | ||
| 4 | +import javax.validation.ConstraintValidatorContext; | ||
| 5 | +import java.util.regex.Matcher; | ||
| 6 | +import java.util.regex.Pattern; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 自定义xss校验注解实现 | ||
| 10 | + * | ||
| 11 | + * @author ruoyi | ||
| 12 | + */ | ||
| 13 | +public class XssValidator implements ConstraintValidator<Xss, String> | ||
| 14 | +{ | ||
| 15 | + private final String HTML_PATTERN = "<(\\S*?)[^>]*>.*?|<.*? />"; | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) | ||
| 19 | + { | ||
| 20 | + return !containsHtml(value); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public boolean containsHtml(String value) | ||
| 24 | + { | ||
| 25 | + Pattern pattern = Pattern.compile(HTML_PATTERN); | ||
| 26 | + Matcher matcher = pattern.matcher(value); | ||
| 27 | + return matcher.matches(); | ||
| 28 | + } | ||
| 29 | +} |
| @@ -5,6 +5,7 @@ import javax.validation.constraints.Size; | @@ -5,6 +5,7 @@ import javax.validation.constraints.Size; | ||
| 5 | import org.apache.commons.lang3.builder.ToStringBuilder; | 5 | import org.apache.commons.lang3.builder.ToStringBuilder; |
| 6 | import org.apache.commons.lang3.builder.ToStringStyle; | 6 | import org.apache.commons.lang3.builder.ToStringStyle; |
| 7 | import com.ruoyi.common.core.domain.BaseEntity; | 7 | import com.ruoyi.common.core.domain.BaseEntity; |
| 8 | +import com.ruoyi.common.xss.Xss; | ||
| 8 | 9 | ||
| 9 | /** | 10 | /** |
| 10 | * 通知公告表 sys_notice | 11 | * 通知公告表 sys_notice |
| @@ -45,6 +46,7 @@ public class SysNotice extends BaseEntity | @@ -45,6 +46,7 @@ public class SysNotice extends BaseEntity | ||
| 45 | this.noticeTitle = noticeTitle; | 46 | this.noticeTitle = noticeTitle; |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 49 | + @Xss(message = "公告标题不能包含脚本字符") | ||
| 48 | @NotBlank(message = "公告标题不能为空") | 50 | @NotBlank(message = "公告标题不能为空") |
| 49 | @Size(min = 0, max = 50, message = "公告标题不能超过50个字符") | 51 | @Size(min = 0, max = 50, message = "公告标题不能超过50个字符") |
| 50 | public String getNoticeTitle() | 52 | public String getNoticeTitle() |
| @@ -3,6 +3,7 @@ package com.ruoyi.system.service.impl; | @@ -3,6 +3,7 @@ package com.ruoyi.system.service.impl; | ||
| 3 | import java.util.ArrayList; | 3 | import java.util.ArrayList; |
| 4 | import java.util.List; | 4 | import java.util.List; |
| 5 | import java.util.stream.Collectors; | 5 | import java.util.stream.Collectors; |
| 6 | +import javax.validation.Validator; | ||
| 6 | import org.slf4j.Logger; | 7 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; | 8 | import org.slf4j.LoggerFactory; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -16,6 +17,7 @@ import com.ruoyi.common.core.domain.entity.SysUser; | @@ -16,6 +17,7 @@ import com.ruoyi.common.core.domain.entity.SysUser; | ||
| 16 | import com.ruoyi.common.exception.ServiceException; | 17 | import com.ruoyi.common.exception.ServiceException; |
| 17 | import com.ruoyi.common.utils.SecurityUtils; | 18 | import com.ruoyi.common.utils.SecurityUtils; |
| 18 | import com.ruoyi.common.utils.StringUtils; | 19 | import com.ruoyi.common.utils.StringUtils; |
| 20 | +import com.ruoyi.common.utils.bean.BeanValidators; | ||
| 19 | import com.ruoyi.common.utils.spring.SpringUtils; | 21 | import com.ruoyi.common.utils.spring.SpringUtils; |
| 20 | import com.ruoyi.system.domain.SysPost; | 22 | import com.ruoyi.system.domain.SysPost; |
| 21 | import com.ruoyi.system.domain.SysUserPost; | 23 | import com.ruoyi.system.domain.SysUserPost; |
| @@ -56,6 +58,9 @@ public class SysUserServiceImpl implements ISysUserService | @@ -56,6 +58,9 @@ public class SysUserServiceImpl implements ISysUserService | ||
| 56 | @Autowired | 58 | @Autowired |
| 57 | private ISysConfigService configService; | 59 | private ISysConfigService configService; |
| 58 | 60 | ||
| 61 | + @Autowired | ||
| 62 | + protected Validator validator; | ||
| 63 | + | ||
| 59 | /** | 64 | /** |
| 60 | * 根据条件分页查询用户列表 | 65 | * 根据条件分页查询用户列表 |
| 61 | * | 66 | * |
| @@ -513,6 +518,7 @@ public class SysUserServiceImpl implements ISysUserService | @@ -513,6 +518,7 @@ public class SysUserServiceImpl implements ISysUserService | ||
| 513 | SysUser u = userMapper.selectUserByUserName(user.getUserName()); | 518 | SysUser u = userMapper.selectUserByUserName(user.getUserName()); |
| 514 | if (StringUtils.isNull(u)) | 519 | if (StringUtils.isNull(u)) |
| 515 | { | 520 | { |
| 521 | + BeanValidators.validateWithException(validator, user); | ||
| 516 | user.setPassword(SecurityUtils.encryptPassword(password)); | 522 | user.setPassword(SecurityUtils.encryptPassword(password)); |
| 517 | user.setCreateBy(operName); | 523 | user.setCreateBy(operName); |
| 518 | this.insertUser(user); | 524 | this.insertUser(user); |
| @@ -521,6 +527,7 @@ public class SysUserServiceImpl implements ISysUserService | @@ -521,6 +527,7 @@ public class SysUserServiceImpl implements ISysUserService | ||
| 521 | } | 527 | } |
| 522 | else if (isUpdateSupport) | 528 | else if (isUpdateSupport) |
| 523 | { | 529 | { |
| 530 | + BeanValidators.validateWithException(validator, user); | ||
| 524 | user.setUpdateBy(operName); | 531 | user.setUpdateBy(operName); |
| 525 | this.updateUser(user); | 532 | this.updateUser(user); |
| 526 | successNum++; | 533 | successNum++; |
-
请 注册 或 登录 后发表评论