form-login.adoc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. = Testing Form Based Authentication
  2. You can easily create a request to test a form based authentication using Spring Security's testing support.
  3. For example, the following `formLogin` xref:servlet/test/mockmvc/request-post-processors.adoc[`RequestPostProcessor`] will submit a POST to "/login" with the username "user", the password "password", and a valid CSRF token:
  4. ====
  5. .Java
  6. [source,java,role="primary"]
  7. ----
  8. mvc
  9. .perform(formLogin())
  10. ----
  11. .Kotlin
  12. [source,kotlin,role="secondary"]
  13. ----
  14. mvc
  15. .perform(formLogin())
  16. ----
  17. ====
  18. It is easy to customize the request.
  19. For example, the following will submit a POST to "/auth" with the username "admin", the password "pass", and a valid CSRF token:
  20. ====
  21. .Java
  22. [source,java,role="primary"]
  23. ----
  24. mvc
  25. .perform(formLogin("/auth").user("admin").password("pass"))
  26. ----
  27. .Kotlin
  28. [source,kotlin,role="secondary"]
  29. ----
  30. mvc
  31. .perform(formLogin("/auth").user("admin").password("pass"))
  32. ----
  33. ====
  34. We can also customize the parameters names that the username and password are included on.
  35. For example, this is the above request modified to include the username on the HTTP parameter "u" and the password on the HTTP parameter "p".
  36. ====
  37. .Java
  38. [source,java,role="primary"]
  39. ----
  40. mvc
  41. .perform(formLogin("/auth").user("u","admin").password("p","pass"))
  42. ----
  43. .Kotlin
  44. [source,kotlin,role="secondary"]
  45. ----
  46. mvc
  47. .perform(formLogin("/auth").user("u","admin").password("p","pass"))
  48. ----
  49. ====