首頁 > 軟體

Spring mvc伺服器端資料校驗實現流程詳解

2020-09-23 12:00:50

B/S 系統中對http 請求資料的校驗多數在使用者端進行,這也是出於簡單及使用者體驗性上考慮,但是在一些安全性要求高的系統中伺服器端校驗是不可缺少的,實際上,幾乎所有的系統,凡是涉及到資料校驗,都需要在伺服器端進行二次校驗。為什麼要在伺服器端進行二次校驗呢?這需要理解使用者端校驗和伺服器端校驗各自的目的。

使用者端校驗,我們主要是為了提高使用者體驗,例如使用者輸入一個郵箱地址,要校驗這個郵箱地址是否合法,沒有必要傳送到伺服器端進行校驗,直接在前端用 js 進行校驗即可。但是大家需要明白的是,前端校驗無法代替後端校驗,前端校驗可以有效的提高使用者體驗,但是無法確保資料完整性,因為在 B/S 架構中,使用者可以方便的拿到請求地址,然後直接傳送請求,傳遞非法引數。
伺服器端校驗,雖然使用者體驗不好,但是可以有效的保證資料安全與完整性。
綜上,實際專案中,兩個一起用。

Spring 支援JSR-303 驗證框架,JSR-303 是 JAVA EE 6 中的一項子規範,叫做 Bean Validation,官方參考實現是 Hibernate Validator(與Hibernate ORM 沒有關係),JSR-303 用於對 Java Bean 中的欄位的值進行驗證。

普通校驗

普通校驗,是這裡最基本的用法。

首先,我們需要加入校驗需要的依賴:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>6.1.0.Final</version>
</dependency>

接下來,在 SpringMVC 的組態檔中設定校驗的 Bean:

<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" id="validatorFactoryBean">
  <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
</bean>
<mvc:annotation-driven validator="validatorFactoryBean"/>

設定時,提供一個 LocalValidatorFactoryBean 的範例,然後 Bean 的校驗使用 HibernateValidator。

這樣,設定就算完成了。

接下來,我們提供一個新增學生的頁面:

<form action="/addstudent" method="post">
  <table>
    <tr>
      <td>學生編號:</td>
      <td><input type="text" name="id"></td>
    </tr>
    <tr>
      <td>學生姓名:</td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr>
      <td>學生郵箱:</td>
      <td><input type="text" name="email"></td>
    </tr>
    <tr>
      <td>學生年齡:</td>
      <td><input type="text" name="age"></td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="提交">
      </td>
    </tr>
  </table>
</form>

在這裡需要提交的資料中,假設學生編號不能為空,學生姓名長度不能超過 10 且不能為空,郵箱地址要合法,年齡不能超過 150。那麼在定義實體類的時候,就可以加入這個判斷條件了。

public class Student {
  @NotNull
  private Integer id;
  @NotNull
  @Size(min = 2,max = 10)
  private String name;
  @Email
  private String email;
  @Max(150)
  private Integer age;
...
}

在這裡:

  • @NotNull 表示這個欄位不能為空
  • @Size 中描述了這個字串長度的限制
  • @Email 表示這個欄位的值必須是一個郵箱地址
  • @Max 表示這個欄位的最大值

定義完成後,接下來,在 Controller 中定義介面:

@Controller
public class StudentController {
  @RequestMapping("/addstudent")
  @ResponseBody
  public void addStudent(@Validated Student student, BindingResult result) {
    if (result != null) {
      //校驗未通過,獲取所有的異常資訊並展示出來
      List<ObjectError> allErrors = result.getAllErrors();
      for (ObjectError allError : allErrors) {
        System.out.println(allError.getObjectName()+":"+allError.getDefaultMessage());
      }
    }
  }
}

在這裡:

  • @Validated 表示 Student 中定義的校驗規則將會生效
  • BindingResult 表示出錯資訊,如果這個變數不為空,表示有錯誤,否則校驗通過。

接下來就可以啟動專案了。存取 jsp 頁面,然後新增 Student,檢視校驗規則是否生效。

預設情況下,列印出來的錯誤資訊時系統預設的錯誤資訊,這個錯誤資訊,我們也可以自定義。自定義方式如下:

由於 properties 檔案中的中文會亂碼,所以需要我們先修改一下 IDEA 設定,點
File–>Settings->Editor–>File Encodings,如下:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6901df99-8a2e-418b-b791-77cff9fc1bb7/Untitled.png

然後定義錯誤提示文字,在 resources 目錄下新建一個 MyMessage.properties 檔案,內容如下:

student.id.notnull=id 不能為空
student.name.notnull=name不能為空
student.name.length=name最小長度為 2 ,最大長度為10
student.email.error=email地址非法
student.age.error=年齡不能超過 150

接下來,在 SpringMVC 設定中,載入這個組態檔:

<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" id="validatorFactoryBean">
  <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
  <property name="validationMessageSource" ref="bundleMessageSource"/>
</bean>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="bundleMessageSource">
  <property name="basenames">
    <list>
      <value>classpath:MyMessage</value>
    </list>
  </property>
  <property name="defaultEncoding" value="UTF-8"/>
  <property name="cacheSeconds" value="300"/>
</bean>
<mvc:annotation-driven validator="validatorFactoryBean"/>

最後,在實體類上,加上校驗出錯時的訊息

public class Student {
  @NotNull(message = "{student.id.notnull}")
  private Integer id;
  @NotNull(message = "{student.name.notnull}")
  @Size(min = 2,max = 10,message = "{student.name.length}")
  private String name;
  @Email(message = "{student.email.error}")
  private String email;
  @Max(value = 150,message = "{student.age.error}")
  private Integer age;
...
}

設定完成後,如果校驗再出錯,就會展示我們自己的出錯資訊了。

分組校驗

由於校驗規則都是定義在實體類上面的,但是,在不同的資料提交環境下,校驗規則可能不一樣。例如,使用者的 id 是自增長的,新增的時候,可以不用傳遞使用者 id,但是修改的時候則必須傳遞使用者 id,這種情況下,就需要使用分組校驗。

分組校驗,首先需要定義校驗組,所謂的校驗組,其實就是空介面:

public interface ValidationGroup1 {
}
public interface ValidationGroup2 {
}

然後,在實體類中,指定每一個校驗規則所屬的組:

public class Student {
  @NotNull(message = "{student.id.notnull}",groups = ValidationGroup1.class)
  private Integer id;
  @NotNull(message = "{student.name.notnull}",groups = {ValidationGroup1.class, ValidationGroup2.class})
  @Size(min = 2,max = 10,message = "{student.name.length}",groups = {ValidationGroup1.class, ValidationGroup2.class})
  private String name;
  @Email(message = "{student.email.error}",groups = {ValidationGroup1.class, ValidationGroup2.class})
  private String email;
  @Max(value = 150,message = "{student.age.error}",groups = {ValidationGroup2.class})
  private Integer age;
...
}

在 group 中指定每一個校驗規則所屬的組,一個規則可以屬於一個組,也可以屬於多個組。

最後,在接收引數的地方,指定校驗組:

@Controller
public class StudentController {
  @RequestMapping("/addstudent")
  @ResponseBody
  public void addStudent(@Validated(ValidationGroup2.class) Student student, BindingResult result) {
    if (result != null) {
      //校驗未通過,獲取所有的異常資訊並展示出來
      List<ObjectError> allErrors = result.getAllErrors();
      for (ObjectError allError : allErrors) {
        System.out.println(allError.getObjectName()+":"+allError.getDefaultMessage());
      }
    }
  }
}

設定完成後,屬於 ValidationGroup2 這個組的校驗規則,才會生效。

校驗註解

@Null 被註解的元素必須為 null

@NotNull 被註解的元素必須不為 null

@AssertTrue 被註解的元素必須為 true

@AssertFalse 被註解的元素必須為 false

@Min(value) 被註解的元素必須是一個數位,其值必須大於等於指定的最小值

@Max(value) 被註解的元素必須是一個數位,其值必須小於等於指定的最大值

@DecimalMin(value) 被註解的元素必須是一個數位,其值必須大於等於指定的最小值

@DecimalMax(value) 被註解的元素必須是一個數位,其值必須小於等於指定的最大值

@Size(max=, min=) 被註解的元素的大小必須在指定的範圍內

@Digits (integer, fraction) 被註解的元素必須是一個數位,其值必須在可接受的範圍內

@Past 被註解的元素必須是一個過去的日期

@Future 被註解的元素必須是一個將來的日期

@Pattern(regex=,flag=) 被註解的元素必須符合指定的正規表示式

@NotBlank(message =) 驗證字串非 null,且長度必須大於0

@Email 被註解的元素必須是電子郵箱地址

@Length(min=,max=) 被註解的字串的大小必須在指定的範圍內

@NotEmpty 被註解的字串的必須非空

@Range(min=,max=,message=) 被註解的元素必須在合適的範圍內

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com