| 
									
										
										
										
											2021-10-29 10:09:04 -06:00
										 |  |  | = WebTestClient Security Setup | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The basic setup looks like this: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 20:29:00 +03:00
										 |  |  | ==== | 
					
						
							|  |  |  | .Java | 
					
						
							|  |  |  | [source,java,role="primary"] | 
					
						
							| 
									
										
										
										
											2021-10-29 10:09:04 -06:00
										 |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-04-11 09:56:19 -03:00
										 |  |  | import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; | 
					
						
							|  |  |  | import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 20:30:10 +01:00
										 |  |  | @ExtendWith(SpringExtension.class) | 
					
						
							| 
									
										
										
										
											2021-10-29 10:09:04 -06:00
										 |  |  | @ContextConfiguration(classes = HelloWebfluxMethodApplication.class) | 
					
						
							|  |  |  | public class HelloWebfluxMethodApplicationTests { | 
					
						
							|  |  |  | 	@Autowired | 
					
						
							|  |  |  | 	ApplicationContext context; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	WebTestClient rest; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 20:30:10 +01:00
										 |  |  | 	@BeforeEach | 
					
						
							| 
									
										
										
										
											2021-10-29 10:09:04 -06:00
										 |  |  | 	public void setup() { | 
					
						
							|  |  |  | 		this.rest = WebTestClient | 
					
						
							|  |  |  | 			.bindToApplicationContext(this.context) | 
					
						
							|  |  |  | 			// add Spring Security test Support | 
					
						
							|  |  |  | 			.apply(springSecurity()) | 
					
						
							|  |  |  | 			.configureClient() | 
					
						
							| 
									
										
										
										
											2022-07-21 20:29:00 +03:00
										 |  |  | 			.filter(basicAuthentication("user", "password")) | 
					
						
							| 
									
										
										
										
											2021-10-29 10:09:04 -06:00
										 |  |  | 			.build(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// ... | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2022-07-21 20:29:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | .Kotlin | 
					
						
							|  |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-04-11 09:56:19 -03:00
										 |  |  | import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity | 
					
						
							|  |  |  | import org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 20:29:00 +03:00
										 |  |  | @ExtendWith(SpringExtension::class) | 
					
						
							|  |  |  | @ContextConfiguration(classes = [HelloWebfluxMethodApplication::class]) | 
					
						
							|  |  |  | class HelloWebfluxMethodApplicationTests { | 
					
						
							|  |  |  |     @Autowired | 
					
						
							|  |  |  |     lateinit var context: ApplicationContext | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lateinit var rest: WebTestClient | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @BeforeEach | 
					
						
							|  |  |  |     fun setup() { | 
					
						
							|  |  |  |         this.rest = WebTestClient | 
					
						
							|  |  |  |             .bindToApplicationContext(this.context) | 
					
						
							|  |  |  |             // add Spring Security test Support | 
					
						
							|  |  |  |             .apply(springSecurity()) | 
					
						
							|  |  |  |             .configureClient() | 
					
						
							|  |  |  |             .filter(basicAuthentication("user", "password")) | 
					
						
							|  |  |  |             .build() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // ... | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | ==== |