文章目录
pom.xml
xml
<!-- 引入validation依赖,完成校验 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.5</version>
</dependency>
ValidatorUtil.java
java
package com.example.util;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 完成一些校验,比如手机号格式是否正确
* 正则
*/
public class ValidatorUtil {
//校验手机号码的正则表达式
private static final Pattern mobile_pattern = Pattern.compile("^[1][3-9][0-9]{9}$");
//编写方法
public static boolean isMobile(String mobile) {
if(!StringUtils.hasText(mobile)) {
return false;
}
Matcher matcher = mobile_pattern.matcher(mobile);
return matcher.matches();
}
//测试校验方法
@Test
public void t1() {
String mobile = "0133333333";
System.out.println(isMobile(mobile));
String mobile2 = "13333333333";
System.out.println(isMobile(mobile2));
}
}
IsMobileValidator.java
java
package com.example.validator;
import org.springframework.util.StringUtils;
import com.example.util.ValidatorUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
//拟定注解 IsMobile 的校验规则, 可以自己根据业务需求来编写..
public class IsMobileValidator
implements ConstraintValidator<IsMobile, String> {
private boolean required = false;
@Override
public void initialize(IsMobile constraintAnnotation) {
//初始化
required = constraintAnnotation.required();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
//必填
if (required) {
return ValidatorUtil.isMobile(value);
} else {//非必填
if (!StringUtils.hasText(value)) {
return true;
} else {
return ValidatorUtil.isMobile(value);
}
}
}
}
IsMobile.java
java
package com.example.validator;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* IsMobile: 开发一个自定义的注解
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {IsMobileValidator.class})
public @interface IsMobile {
String message() default "手机号码格式错误";
boolean required() default true;
Class<?>[] groups() default { };//默认参数
Class<? extends Payload>[] payload() default { };//默认参数
}
LoginVo.java
java
package com.example.vo;
import com.example.validator.IsMobile;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotNull;
/**
* 接收用户登录时,发送的信息(moblie,password)
*/
@Data
public class LoginVo {
//对LoginVo的属性值进行约束
@NotNull
@IsMobile
private String mobile;
@NotNull
@Length(min = 32)
private String password;
}
LoginController.java
java
package com.example.controller;
import com.example.service.UserService;
import com.example.vo.LoginVo;
import com.example.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
//装配UserService
@Resource
private UserService userService;
//编写方法,可以进入到登录页面
@RequestMapping("/toLogin")
public String toLogin() {
return "login";//到templates/login.html
}
//处理用户登录请求
@RequestMapping("/doLogin")
@ResponseBody
public RespBean doLogin(@Valid LoginVo loginVo,
HttpServletRequest request,
HttpServletResponse response) {
System.out.println("controller层:"+loginVo);
//log.info("{}",loginVo);
return userService.doLogin(loginVo,request,response);
}
}