修改登录验证:手机号除admin 外长度必须为11
New file |
| | |
| | | package com.dy.common.aop; |
| | | |
| | | import jakarta.validation.Constraint; |
| | | import jakarta.validation.Payload; |
| | | import java.lang.annotation.*; |
| | | |
| | | @Documented |
| | | @Constraint(validatedBy = CheckLengthValidator.class) |
| | | @Target({ElementType.FIELD, ElementType.METHOD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface CheckLength { |
| | | |
| | | int min() default 11; |
| | | int max() default 12; |
| | | |
| | | String message() default "手机号长度必须是11位"; |
| | | |
| | | Class<?>[] groups() default {}; |
| | | |
| | | Class<? extends Payload>[] payload() default {}; |
| | | } |
New file |
| | |
| | | package com.dy.common.aop; |
| | | |
| | | import jakarta.validation.ConstraintValidator; |
| | | import jakarta.validation.ConstraintValidatorContext; |
| | | |
| | | public class CheckLengthValidator implements ConstraintValidator<CheckLength, String> { |
| | | |
| | | private int min; |
| | | private int max; |
| | | @Override |
| | | public void initialize(CheckLength constraintAnnotation) { |
| | | this.min = constraintAnnotation.min(); |
| | | this.max = constraintAnnotation.max(); |
| | | } |
| | | @Override |
| | | public boolean isValid(String value, ConstraintValidatorContext context) { |
| | | if ("admin".equals(value)) { |
| | | return true; // 如果是admin,不进行长度校验 |
| | | } |
| | | return value != null && value.length() >= min && value.length() <= max; |
| | | } |
| | | } |
| | |
| | | package com.dy.sso.busi; |
| | | |
| | | import com.dy.common.aop.CheckLength; |
| | | import jakarta.validation.constraints.NotEmpty; |
| | | import lombok.*; |
| | | import org.hibernate.validator.constraints.Length; |
| | |
| | | public class LoginVo { |
| | | @NotEmpty(message = "手机号不能为空") //不能为空也不能为null |
| | | //@NotNull(message = "手机号不能为空") //不能为null但是可以为空 |
| | | @Length(message = "手机号必须{min}位", min = 11, max = 11) |
| | | // @Length(message = "手机号必须{min}位", min = 11, max = 11) |
| | | @CheckLength(message = "手机号必须{min}位", min = 11, max = 11) |
| | | public String phone ; |
| | | |
| | | @NotEmpty(message = "密码不能为空") //不能为空也不能为null |
| | |
| | | * @return 登录用户值对象 |
| | | */ |
| | | @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据 |
| | | @Log("用户登录(json)") |
| | | @Log("用户登录") |
| | | public BaseResponse<UserVo> login(@RequestBody @Valid LoginVo vo,BindingResult bindingResult, |
| | | HttpSession session) { |
| | | try { |
| | |
| | | * @return 登录用户值对象 |
| | | */ |
| | | @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//前端提交form表单数据 |
| | | @Log("用户登录(form)") |
| | | public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo, BindingResult bindingResult,HttpSession session){ |
| | | try{ |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |