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