2
0

result-matchers.adoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. == SecurityMockMvcResultMatchers
  2. At times it is desirable to make various security related assertions about a request.
  3. To accommodate this need, Spring Security Test support implements Spring MVC Test's `ResultMatcher` interface.
  4. In order to use Spring Security's `ResultMatcher` implementations ensure the following static import is used:
  5. ====
  6. .Java
  7. [source,java,role="primary"]
  8. ----
  9. import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
  10. ----
  11. .Kotlin
  12. [source,kotlin,role="secondary"]
  13. ----
  14. import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*
  15. ----
  16. ====
  17. === Unauthenticated Assertion
  18. At times it may be valuable to assert that there is no authenticated user associated with the result of a `MockMvc` invocation.
  19. For example, you might want to test submitting an invalid username and password and verify that no user is authenticated.
  20. You can easily do this with Spring Security's testing support using something like the following:
  21. ====
  22. .Java
  23. [source,java,role="primary"]
  24. ----
  25. mvc
  26. .perform(formLogin().password("invalid"))
  27. .andExpect(unauthenticated());
  28. ----
  29. .Kotlin
  30. [source,kotlin,role="secondary"]
  31. ----
  32. mvc
  33. .perform(formLogin().password("invalid"))
  34. .andExpect { unauthenticated() }
  35. ----
  36. ====
  37. === Authenticated Assertion
  38. It is often times that we must assert that an authenticated user exists.
  39. For example, we may want to verify that we authenticated successfully.
  40. We could verify that a form based login was successful with the following snippet of code:
  41. ====
  42. .Java
  43. [source,java,role="primary"]
  44. ----
  45. mvc
  46. .perform(formLogin())
  47. .andExpect(authenticated());
  48. ----
  49. .Kotlin
  50. [source,kotlin,role="secondary"]
  51. ----
  52. mvc
  53. .perform(formLogin())
  54. .andExpect { authenticated() }
  55. ----
  56. ====
  57. If we wanted to assert the roles of the user, we could refine our previous code as shown below:
  58. ====
  59. .Java
  60. [source,java,role="primary"]
  61. ----
  62. mvc
  63. .perform(formLogin().user("admin"))
  64. .andExpect(authenticated().withRoles("USER","ADMIN"));
  65. ----
  66. .Kotlin
  67. [source,kotlin,role="secondary"]
  68. ----
  69. mvc
  70. .perform(formLogin())
  71. .andExpect { authenticated().withRoles("USER","ADMIN") }
  72. ----
  73. ====
  74. Alternatively, we could verify the username:
  75. ====
  76. .Java
  77. [source,java,role="primary"]
  78. ----
  79. mvc
  80. .perform(formLogin().user("admin"))
  81. .andExpect(authenticated().withUsername("admin"));
  82. ----
  83. .Kotlin
  84. [source,kotlin,role="secondary"]
  85. ----
  86. mvc
  87. .perform(formLogin().user("admin"))
  88. .andExpect { authenticated().withUsername("admin") }
  89. ----
  90. ====
  91. We can also combine the assertions:
  92. ====
  93. .Java
  94. [source,java,role="primary"]
  95. ----
  96. mvc
  97. .perform(formLogin().user("admin"))
  98. .andExpect(authenticated().withUsername("admin").withRoles("USER", "ADMIN"));
  99. ----
  100. .Kotlin
  101. [source,kotlin,role="secondary"]
  102. ----
  103. mvc
  104. .perform(formLogin().user("admin"))
  105. .andExpect { authenticated().withUsername("admin").withRoles("USER", "ADMIN") }
  106. ----
  107. ====
  108. We can also make arbitrary assertions on the authentication
  109. ====
  110. .Java
  111. [source,java,role="primary"]
  112. ----
  113. mvc
  114. .perform(formLogin())
  115. .andExpect(authenticated().withAuthentication(auth ->
  116. assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken.class)));
  117. ----
  118. .Kotlin
  119. [source,kotlin,role="secondary"]
  120. ----
  121. mvc
  122. .perform(formLogin())
  123. .andExpect {
  124. authenticated().withAuthentication { auth ->
  125. assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken::class.java) }
  126. }
  127. }
  128. ----
  129. ====