use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method basicWithAnonymous.
@Test
public void basicWithAnonymous() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("rob", "rob", "ROLE_USER", "ROLE_ADMIN")));
this.http.httpBasic().and().anonymous();
this.http.authenticationManager(this.authenticationManager);
ServerHttpSecurity.AuthorizeExchangeSpec authorize = this.http.authorizeExchange();
authorize.anyExchange().hasAuthority("ROLE_ADMIN");
WebTestClient client = buildClient();
// @formatter:off
EntityExchangeResult<String> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("rob", "rob")).exchange().expectStatus().isOk().expectHeader().valueMatches(HttpHeaders.CACHE_CONTROL, ".+").expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
// @formatter:on
assertThat(result.getResponseCookies().getFirst("SESSION")).isNull();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method getWhenAnonymousConfiguredThenAuthenticationIsAnonymous.
@Test
public void getWhenAnonymousConfiguredThenAuthenticationIsAnonymous() {
SecurityWebFilterChain securityFilterChain = this.http.anonymous(withDefaults()).build();
// @formatter:off
WebTestClient client = WebTestClientBuilder.bindToControllerAndWebFilters(AnonymousAuthenticationWebFilterTests.HttpMeController.class, securityFilterChain).build();
client.get().uri("/me").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("anonymousUser");
// @formatter:on
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method anonymous.
@Test
public void anonymous() {
// @formatter:off
SecurityWebFilterChain securityFilterChain = this.http.anonymous().and().build();
WebTestClient client = WebTestClientBuilder.bindToControllerAndWebFilters(AnonymousAuthenticationWebFilterTests.HttpMeController.class, securityFilterChain).build();
client.get().uri("/me").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("anonymousUser");
// @formatter:on
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method basicWithCustomRealmName.
@Test
public void basicWithCustomRealmName() {
this.http.securityContextRepository(new WebSessionServerSecurityContextRepository());
HttpBasicServerAuthenticationEntryPoint authenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint();
authenticationEntryPoint.setRealm("myrealm");
this.http.httpBasic().authenticationEntryPoint(authenticationEntryPoint);
this.http.authenticationManager(this.authenticationManager);
ServerHttpSecurity.AuthorizeExchangeSpec authorize = this.http.authorizeExchange();
authorize.anyExchange().authenticated();
WebTestClient client = buildClient();
// @formatter:off
EntityExchangeResult<String> result = client.get().uri("/").exchange().expectStatus().isUnauthorized().expectHeader().value(HttpHeaders.WWW_AUTHENTICATE, (value) -> assertThat(value).contains("myrealm")).expectBody(String.class).returnResult();
// @formatter:on
assertThat(result.getResponseCookies().getFirst("SESSION")).isNull();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method defaults.
@Test
public void defaults() {
TestPublisher<SecurityContext> securityContext = TestPublisher.create();
given(this.contextRepository.load(any())).willReturn(securityContext.mono());
this.http.securityContextRepository(this.contextRepository);
WebTestClient client = buildClient();
// @formatter:off
FluxExchangeResult<String> result = client.get().uri("/").exchange().expectHeader().valueMatches(HttpHeaders.CACHE_CONTROL, ".+").returnResult(String.class);
// @formatter:on
assertThat(result.getResponseCookies()).isEmpty();
// there is no need to try and load the SecurityContext by default
securityContext.assertWasNotSubscribed();
}
Aggregations