Remove ExchangeFilterFunctions

Issue: gh-5612
This commit is contained in:
Rob Winch 2018-07-30 14:45:14 -05:00
parent 262c1a77c6
commit afa2d9cbc7
11 changed files with 55 additions and 103 deletions

View File

@ -68,8 +68,6 @@ import java.security.Principal;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/** /**
* @author Rob Winch * @author Rob Winch
@ -122,11 +120,10 @@ public class EnableWebFluxSecurityTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(this.springSecurityFilterChain) .bindToWebFilters(this.springSecurityFilterChain)
.filter(basicAuthentication())
.build(); .build();
FluxExchangeResult<String> result = client.get() FluxExchangeResult<String> result = client.get()
.attributes(basicAuthenticationCredentials("user", "password")) .headers(headers -> headers.setBasicAuth("user", "password"))
.exchange() .exchange()
.expectStatus() .expectStatus()
.isOk() .isOk()
@ -171,13 +168,12 @@ public class EnableWebFluxSecurityTests {
.flatMap( principal -> exchange.getResponse() .flatMap( principal -> exchange.getResponse()
.writeWith(Mono.just(toDataBuffer(principal.getName())))) .writeWith(Mono.just(toDataBuffer(principal.getName()))))
) )
.filter(basicAuthentication())
.build(); .build();
client client
.get() .get()
.uri("/") .uri("/")
.attributes(basicAuthenticationCredentials("user", "password")) .headers(headers -> headers.setBasicAuth("user", "password"))
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).consumeWith( result -> assertThat(result.getResponseBody()).isEqualTo("user")); .expectBody(String.class).consumeWith( result -> assertThat(result.getResponseBody()).isEqualTo("user"));
@ -208,13 +204,12 @@ public class EnableWebFluxSecurityTests {
.flatMap( principal -> exchange.getResponse() .flatMap( principal -> exchange.getResponse()
.writeWith(Mono.just(toDataBuffer(principal.getName())))) .writeWith(Mono.just(toDataBuffer(principal.getName()))))
) )
.filter(basicAuthentication())
.build(); .build();
client client
.get() .get()
.uri("/") .uri("/")
.attributes(basicAuthenticationCredentials("user", "password")) .headers(headers -> headers.setBasicAuth("user", "password"))
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).consumeWith( result -> assertThat(result.getResponseBody()).isEqualTo("user")); .expectBody(String.class).consumeWith( result -> assertThat(result.getResponseBody()).isEqualTo("user"));

View File

@ -25,8 +25,6 @@ import org.springframework.security.web.server.authentication.RedirectServerAuth
import org.springframework.security.web.server.authorization.HttpStatusServerAccessDeniedHandler; import org.springframework.security.web.server.authorization.HttpStatusServerAccessDeniedHandler;
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler; import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/** /**
* @author Denys Ivano * @author Denys Ivano
@ -96,13 +94,12 @@ public class ExceptionHandlingSpecTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(securityWebFilter) .bindToWebFilters(securityWebFilter)
.filter(basicAuthentication())
.build(); .build();
client client
.get() .get()
.uri("/admin") .uri("/admin")
.attributes(basicAuthenticationCredentials("user", "password")) .headers(headers -> headers.setBasicAuth("user", "password"))
.exchange() .exchange()
.expectStatus().isForbidden(); .expectStatus().isForbidden();
} }
@ -122,13 +119,12 @@ public class ExceptionHandlingSpecTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(securityWebFilter) .bindToWebFilters(securityWebFilter)
.filter(basicAuthentication())
.build(); .build();
client client
.get() .get()
.uri("/admin") .uri("/admin")
.attributes(basicAuthenticationCredentials("user", "password")) .headers(headers -> headers.setBasicAuth("user", "password"))
.exchange() .exchange()
.expectStatus().isBadRequest(); .expectStatus().isBadRequest();
} }

View File

@ -40,7 +40,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/** /**
* @author Rob Winch * @author Rob Winch
@ -92,12 +91,9 @@ public class ServerHttpSecurityTests {
WebTestClient client = buildClient(); WebTestClient client = buildClient();
EntityExchangeResult<String> result = client EntityExchangeResult<String> result = client.get()
.mutate()
.filter(basicAuthentication("rob", "rob"))
.build()
.get()
.uri("/") .uri("/")
.headers(headers -> headers.setBasicAuth("rob", "rob"))
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectHeader().valueMatches(HttpHeaders.CACHE_CONTROL, ".+") .expectHeader().valueMatches(HttpHeaders.CACHE_CONTROL, ".+")

View File

@ -23,12 +23,10 @@ import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenRespon
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import static org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse; import static org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
/** /**
* An implementation of an {@link ReactiveOAuth2AccessTokenResponseClient} that &quot;exchanges&quot; * An implementation of an {@link ReactiveOAuth2AccessTokenResponseClient} that &quot;exchanges&quot;
@ -49,7 +47,6 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
*/ */
public class WebClientReactiveAuthorizationCodeTokenResponseClient implements ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> { public class WebClientReactiveAuthorizationCodeTokenResponseClient implements ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
private WebClient webClient = WebClient.builder() private WebClient webClient = WebClient.builder()
.filter(ExchangeFilterFunctions.basicAuthentication())
.build(); .build();
@Override @Override
@ -66,7 +63,7 @@ public class WebClientReactiveAuthorizationCodeTokenResponseClient implements Re
return this.webClient.post() return this.webClient.post()
.uri(tokenUri) .uri(tokenUri)
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.attributes(basicAuthenticationCredentials(clientRegistration.getClientId(), clientRegistration.getClientSecret())) .headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.body(body) .body(body)
.exchange() .exchange()
.flatMap(response -> response.body(oauth2AccessTokenResponse())) .flatMap(response -> response.body(oauth2AccessTokenResponse()))

View File

@ -15,19 +15,16 @@
*/ */
package sample; package sample;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
/** /**
* @author Rob Winch * @author Rob Winch
@ -37,13 +34,8 @@ import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloWebfluxMethodApplicationITests { public class HelloWebfluxMethodApplicationITests {
WebTestClient rest;
@Autowired @Autowired
public void setRest(WebTestClient rest) { WebTestClient rest;
this.rest = rest
.mutateWith((b, h, c) -> b.filter(ExchangeFilterFunctions.basicAuthentication()));
}
@Test @Test
@ -60,7 +52,7 @@ public class HelloWebfluxMethodApplicationITests {
this.rest this.rest
.get() .get()
.uri("/message") .uri("/message")
.attributes(robsCredentials()) .headers(robsCredentials())
.exchange() .exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN); .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
} }
@ -70,18 +62,18 @@ public class HelloWebfluxMethodApplicationITests {
this.rest this.rest
.get() .get()
.uri("/message") .uri("/message")
.attributes(adminCredentials()) .headers(adminCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello World!"); .expectBody(String.class).isEqualTo("Hello World!");
} }
private Consumer<Map<String, Object>> robsCredentials() { private Consumer<HttpHeaders> robsCredentials() {
return basicAuthenticationCredentials("rob", "rob"); return httpHeaders -> httpHeaders.setBasicAuth("rob", "rob");
} }
private Consumer<Map<String, Object>> adminCredentials() { private Consumer<HttpHeaders> adminCredentials() {
return basicAuthenticationCredentials("admin", "admin"); return httpHeaders -> httpHeaders.setBasicAuth("admin", "admin");
} }
} }

View File

@ -17,10 +17,7 @@ package sample;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
@ -28,6 +25,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@ -48,7 +46,6 @@ public class HelloWebfluxMethodApplicationTests {
.bindToApplicationContext(context) .bindToApplicationContext(context)
.apply(springSecurity()) .apply(springSecurity())
.configureClient() .configureClient()
.filter(basicAuthentication())
.build(); .build();
} }
@ -68,7 +65,7 @@ public class HelloWebfluxMethodApplicationTests {
this.rest this.rest
.get() .get()
.uri("/message") .uri("/message")
.attributes(robsCredentials()) .headers(robsCredentials())
.exchange() .exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN); .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
} }
@ -78,7 +75,7 @@ public class HelloWebfluxMethodApplicationTests {
this.rest this.rest
.get() .get()
.uri("/message") .uri("/message")
.attributes(adminCredentials()) .headers(adminCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello World!"); .expectBody(String.class).isEqualTo("Hello World!");
@ -130,11 +127,11 @@ public class HelloWebfluxMethodApplicationTests {
.expectBody(String.class).isEqualTo("Hello World!"); .expectBody(String.class).isEqualTo("Hello World!");
} }
private Consumer<Map<String, Object>> robsCredentials() { private Consumer<HttpHeaders> robsCredentials() {
return basicAuthenticationCredentials("rob", "rob"); return httpHeaders -> httpHeaders.setBasicAuth("rob", "rob");
} }
private Consumer<Map<String, Object>> adminCredentials() { private Consumer<HttpHeaders> adminCredentials() {
return basicAuthenticationCredentials("admin", "admin"); return httpHeaders -> httpHeaders.setBasicAuth("admin", "admin");
} }
} }

View File

@ -15,18 +15,15 @@
*/ */
package sample; package sample;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
/** /**
* @author Rob Winch * @author Rob Winch
@ -36,13 +33,8 @@ import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloWebfluxApplicationITests { public class HelloWebfluxApplicationITests {
WebTestClient rest;
@Autowired @Autowired
public void setRest(WebTestClient rest) { WebTestClient rest;
this.rest = rest
.mutateWith((b, h, c) -> b.filter(ExchangeFilterFunctions.basicAuthentication()));
}
@Test @Test
public void basicWhenNoCredentialsThenUnauthorized() throws Exception { public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
@ -58,7 +50,7 @@ public class HelloWebfluxApplicationITests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(userCredentials()) .headers(userCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
@ -69,17 +61,17 @@ public class HelloWebfluxApplicationITests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(invalidCredentials()) .headers(invalidCredentials())
.exchange() .exchange()
.expectStatus().isUnauthorized() .expectStatus().isUnauthorized()
.expectBody().isEmpty(); .expectBody().isEmpty();
} }
private Consumer<Map<String, Object>> userCredentials() { private Consumer<HttpHeaders> userCredentials() {
return basicAuthenticationCredentials("user", "user"); return httpHeaders -> httpHeaders.setBasicAuth("user", "user");
} }
private Consumer<Map<String, Object>> invalidCredentials() { private Consumer<HttpHeaders> invalidCredentials() {
return basicAuthenticationCredentials("user", "INVALID"); return httpHeaders -> httpHeaders.setBasicAuth("user", "INVALID");
} }
} }

View File

@ -17,10 +17,7 @@ package sample;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
@ -29,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@ -49,7 +47,6 @@ public class HelloWebfluxApplicationTests {
.bindToApplicationContext(context) .bindToApplicationContext(context)
.apply(springSecurity()) .apply(springSecurity())
.configureClient() .configureClient()
.filter(basicAuthentication())
.build(); .build();
} }
@ -67,7 +64,7 @@ public class HelloWebfluxApplicationTests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(userCredentials()) .headers(userCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
@ -78,7 +75,7 @@ public class HelloWebfluxApplicationTests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(invalidCredentials()) .headers(invalidCredentials())
.exchange() .exchange()
.expectStatus().isUnauthorized() .expectStatus().isUnauthorized()
.expectBody().isEmpty(); .expectBody().isEmpty();
@ -106,11 +103,11 @@ public class HelloWebfluxApplicationTests {
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
} }
private Consumer<Map<String, Object>> userCredentials() { private Consumer<HttpHeaders> userCredentials() {
return basicAuthenticationCredentials("user", "user"); return httpHeaders -> httpHeaders.setBasicAuth("user", "user");
} }
private Consumer<Map<String, Object>> invalidCredentials() { private Consumer<HttpHeaders> invalidCredentials() {
return basicAuthenticationCredentials("user", "INVALID"); return httpHeaders -> httpHeaders.setBasicAuth("user", "INVALID");
} }
} }

View File

@ -15,15 +15,13 @@
*/ */
package sample; package sample;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
@ -58,7 +56,7 @@ public class HelloWebfluxFnApplicationITests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(userCredentials()) .headers(userCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
@ -69,17 +67,17 @@ public class HelloWebfluxFnApplicationITests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(invalidCredentials()) .headers(invalidCredentials())
.exchange() .exchange()
.expectStatus().isUnauthorized() .expectStatus().isUnauthorized()
.expectBody().isEmpty(); .expectBody().isEmpty();
} }
private Consumer<Map<String, Object>> userCredentials() { private Consumer<HttpHeaders> userCredentials() {
return basicAuthenticationCredentials("user", "user"); return httpHeaders -> httpHeaders.setBasicAuth("user", "user");
} }
private Consumer<Map<String, Object>> invalidCredentials() { private Consumer<HttpHeaders> invalidCredentials() {
return basicAuthenticationCredentials("user", "INVALID"); return httpHeaders -> httpHeaders.setBasicAuth("user", "INVALID");
} }
} }

View File

@ -17,10 +17,7 @@ package sample;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity; import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.junit.Test; import org.junit.Test;
@ -29,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@ -50,7 +48,6 @@ public class HelloWebfluxFnApplicationTests {
.bindToApplicationContext(context) .bindToApplicationContext(context)
.apply(springSecurity()) .apply(springSecurity())
.configureClient() .configureClient()
.filter(basicAuthentication())
.build(); .build();
} }
@ -68,7 +65,7 @@ public class HelloWebfluxFnApplicationTests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(userCredentials()) .headers(userCredentials())
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
@ -79,7 +76,7 @@ public class HelloWebfluxFnApplicationTests {
this.rest this.rest
.get() .get()
.uri("/") .uri("/")
.attributes(invalidCredentials()) .headers(invalidCredentials())
.exchange() .exchange()
.expectStatus().isUnauthorized() .expectStatus().isUnauthorized()
.expectBody().isEmpty(); .expectBody().isEmpty();
@ -107,11 +104,11 @@ public class HelloWebfluxFnApplicationTests {
.expectBody().json("{\"message\":\"Hello user!\"}"); .expectBody().json("{\"message\":\"Hello user!\"}");
} }
private Consumer<Map<String, Object>> userCredentials() { private Consumer<HttpHeaders> userCredentials() {
return basicAuthenticationCredentials("user", "user"); return httpHeaders -> httpHeaders.setBasicAuth("user", "user");
} }
private Consumer<Map<String, Object>> invalidCredentials() { private Consumer<HttpHeaders> invalidCredentials() {
return basicAuthenticationCredentials("user", "INVALID"); return httpHeaders -> httpHeaders.setBasicAuth("user", "INVALID");
} }
} }

View File

@ -43,8 +43,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
/** /**
@ -101,13 +99,12 @@ public class AuthenticationWebFilterTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(this.filter) .bindToWebFilters(this.filter)
.filter(basicAuthentication())
.build(); .build();
EntityExchangeResult<String> result = client EntityExchangeResult<String> result = client
.get() .get()
.uri("/") .uri("/")
.attributes(basicAuthenticationCredentials("test", "this")) .headers(headers -> headers.setBasicAuth("test", "this"))
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok")) .expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))
@ -123,13 +120,12 @@ public class AuthenticationWebFilterTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(this.filter) .bindToWebFilters(this.filter)
.filter(basicAuthentication())
.build(); .build();
EntityExchangeResult<Void> result = client EntityExchangeResult<Void> result = client
.get() .get()
.uri("/") .uri("/")
.attributes(basicAuthenticationCredentials("test", "this")) .headers(headers -> headers.setBasicAuth("test", "this"))
.exchange() .exchange()
.expectStatus().isUnauthorized() .expectStatus().isUnauthorized()
.expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"") .expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"")
@ -231,13 +227,12 @@ public class AuthenticationWebFilterTests {
WebTestClient client = WebTestClientBuilder WebTestClient client = WebTestClientBuilder
.bindToWebFilters(this.filter) .bindToWebFilters(this.filter)
.filter(basicAuthentication())
.build(); .build();
EntityExchangeResult<String> result = client EntityExchangeResult<String> result = client
.get() .get()
.uri("/") .uri("/")
.attributes(basicAuthenticationCredentials("test", "this")) .headers(headers -> headers.setBasicAuth("test", "this"))
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok")) .expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))