| 
									
										
										
										
											2023-07-23 14:35:51 +05:30
										 |  |  | = SecurityMockMvcResultMatchers | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | At times it is desirable to make various security related assertions about a request. | 
					
						
							|  |  |  | To accommodate this need, Spring Security Test support implements Spring MVC Test's `ResultMatcher` interface. | 
					
						
							|  |  |  | In order to use Spring Security's `ResultMatcher` implementations ensure the following static import is used: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*; | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.* | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 14:07:40 -05:00
										 |  |  | == Unauthenticated Assertion | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | At times it may be valuable to assert that there is no authenticated user associated with the result of a `MockMvc` invocation. | 
					
						
							|  |  |  | For example, you might want to test submitting an invalid username and password and verify that no user is authenticated. | 
					
						
							|  |  |  | You can easily do this with Spring Security's testing support using something like the following: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin().password("invalid")) | 
					
						
							|  |  |  | 	.andExpect(unauthenticated()); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin().password("invalid")) | 
					
						
							|  |  |  |     .andExpect { unauthenticated() } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 14:07:40 -05:00
										 |  |  | == Authenticated Assertion | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | It is often times that we must assert that an authenticated user exists. | 
					
						
							|  |  |  | For example, we may want to verify that we authenticated successfully. | 
					
						
							|  |  |  | We could verify that a form based login was successful with the following snippet of code: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin()) | 
					
						
							|  |  |  | 	.andExpect(authenticated()); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin()) | 
					
						
							|  |  |  |     .andExpect { authenticated() } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | If we wanted to assert the roles of the user, we could refine our previous code as shown below: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin().user("admin")) | 
					
						
							|  |  |  | 	.andExpect(authenticated().withRoles("USER","ADMIN")); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin()) | 
					
						
							|  |  |  |     .andExpect { authenticated().withRoles("USER","ADMIN") } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | Alternatively, we could verify the username: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin().user("admin")) | 
					
						
							|  |  |  | 	.andExpect(authenticated().withUsername("admin")); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin().user("admin")) | 
					
						
							|  |  |  |     .andExpect { authenticated().withUsername("admin") } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | We can also combine the assertions: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin().user("admin")) | 
					
						
							|  |  |  | 	.andExpect(authenticated().withUsername("admin").withRoles("USER", "ADMIN")); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin().user("admin")) | 
					
						
							|  |  |  |     .andExpect { authenticated().withUsername("admin").withRoles("USER", "ADMIN") } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | We can also make arbitrary assertions on the authentication | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,java,role="primary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  | 	.perform(formLogin()) | 
					
						
							|  |  |  | 	.andExpect(authenticated().withAuthentication(auth -> | 
					
						
							|  |  |  | 		assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken.class))); | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2021-10-29 12:34:29 -06:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | mvc | 
					
						
							|  |  |  |     .perform(formLogin()) | 
					
						
							|  |  |  |     .andExpect { | 
					
						
							|  |  |  |         authenticated().withAuthentication { auth -> | 
					
						
							|  |  |  |             assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken::class.java) } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:30:41 -05:00
										 |  |  | ====== |