背景
在前台表单验证的时候,通常会校验一些数据的可行性,比如是否为空,长度,身份证,邮箱等等,那么这样是否是安全的呢,答案是否定的。因为也可以通过模拟前台请求等工具来直接提交到后台,比如postman这样的工具,那么遇到这样的问题怎么办呢,我们可以在后台也做相应的校验。后台就是校验数据的最后一道防线
环境
步骤
1.pom.xml文件
SpringBoot框架真的是太方便了,只要引入了web-start
的依赖,其内就包含着**Spring Validator**的依赖,我们可以直接上手应用,不必再去加什么配置之类的
在pom.xml文件中加入web-start
的依赖,代码如下:
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.创造实体类
@Data
public class User implements Serializable {
private BigInteger id;
@JsonIgnore
@Length(min = 6,max = 20,message = "密码长度必须在6~20个字符之间")
private String password;
@Length(min =