使用CSRF保護進行測試

當測試任何非安全HTTP方法並使用Spring Security的CSRF保護時,您必須在請求中包含有效的CSRF令牌。要將有效的CSRF令牌指定為請求引數,請使用CSRF RequestPostProcessor,如下所示

  • Java

  • Kotlin

mvc
	.perform(post("/").with(csrf()))
mvc.post("/") {
    with(csrf())
}

如果您願意,可以改為在請求頭中包含CSRF令牌

  • Java

  • Kotlin

mvc
	.perform(post("/").with(csrf().asHeader()))
mvc.post("/") {
    with(csrf().asHeader())
}

您還可以透過使用以下方式測試提供無效的CSRF令牌

  • Java

  • Kotlin

mvc
	.perform(post("/").with(csrf().useInvalidToken()))
mvc.post("/") {
    with(csrf().useInvalidToken())
}
© . This site is unofficial and not affiliated with VMware.