mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-02 00:32:15 +00:00
Use basicAuthenticationCredentials
This commit is contained in:
parent
65b968f04a
commit
21f8ee7f36
@ -16,6 +16,8 @@
|
|||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -26,9 +28,9 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
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.ExchangeFilterFunction;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
||||||
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
@ -48,6 +50,7 @@ public class HelloWebfluxApplicationITests {
|
|||||||
this.rest = WebTestClient.bindToServer()
|
this.rest = WebTestClient.bindToServer()
|
||||||
.responseTimeout(Duration.ofDays(1))
|
.responseTimeout(Duration.ofDays(1))
|
||||||
.baseUrl("http://localhost:" + this.port)
|
.baseUrl("http://localhost:" + this.port)
|
||||||
|
.filter(basicAuthentication())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,11 +67,9 @@ public class HelloWebfluxApplicationITests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenValidCredentialsThenOk() throws Exception {
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(userCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().json("{\"message\":\"Hello user!\"}");
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
||||||
@ -77,21 +78,19 @@ public class HelloWebfluxApplicationITests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(invalidPassword())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(invalidCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isUnauthorized()
|
.expectStatus().isUnauthorized()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private Consumer<Map<String, Object>> userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthenticationCredentials("user","user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction invalidPassword() {
|
private Consumer<Map<String, Object>> invalidCredentials() {
|
||||||
return basicAuthentication("user","INVALID");
|
return basicAuthenticationCredentials("user","INVALID");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@
|
|||||||
*/
|
*/
|
||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -27,11 +30,11 @@ import org.springframework.test.context.ActiveProfiles;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
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.ExchangeFilterFunction;
|
|
||||||
|
|
||||||
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.basicAuthentication;
|
||||||
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
@ -51,6 +54,8 @@ public class HelloWebfluxApplicationTests {
|
|||||||
this.rest = WebTestClient
|
this.rest = WebTestClient
|
||||||
.bindToApplicationContext(this.context)
|
.bindToApplicationContext(this.context)
|
||||||
.apply(springSecurity())
|
.apply(springSecurity())
|
||||||
|
.configureClient()
|
||||||
|
.filter(basicAuthentication())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,11 +71,9 @@ public class HelloWebfluxApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenValidCredentialsThenOk() throws Exception {
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(userCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().json("{\"message\":\"Hello user!\"}");
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
||||||
@ -79,11 +82,9 @@ public class HelloWebfluxApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(invalidPassword())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(invalidCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isUnauthorized()
|
.expectStatus().isUnauthorized()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
@ -106,11 +107,11 @@ public class HelloWebfluxApplicationTests {
|
|||||||
.expectStatus().isUnauthorized();
|
.expectStatus().isUnauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private Consumer<Map<String, Object>> userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthenticationCredentials("user","user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction invalidPassword() {
|
private Consumer<Map<String, Object>> invalidCredentials() {
|
||||||
return basicAuthentication("user","INVALID");
|
return basicAuthenticationCredentials("user","INVALID");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -26,9 +28,9 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
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.ExchangeFilterFunction;
|
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
||||||
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
@ -48,6 +50,7 @@ public class HelloWebfluxFnApplicationITests {
|
|||||||
this.rest = WebTestClient.bindToServer()
|
this.rest = WebTestClient.bindToServer()
|
||||||
.responseTimeout(Duration.ofDays(1))
|
.responseTimeout(Duration.ofDays(1))
|
||||||
.baseUrl("http://localhost:" + this.port)
|
.baseUrl("http://localhost:" + this.port)
|
||||||
|
.filter(basicAuthentication())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,11 +66,9 @@ public class HelloWebfluxFnApplicationITests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenValidCredentialsThenOk() throws Exception {
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(userCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().json("{\"message\":\"Hello user!\"}");
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
||||||
@ -76,21 +77,19 @@ public class HelloWebfluxFnApplicationITests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(invalidPassword())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(invalidCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isUnauthorized()
|
.expectStatus().isUnauthorized()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private Consumer<Map<String, Object>> userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthenticationCredentials("user","user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction invalidPassword() {
|
private Consumer<Map<String, Object>> invalidCredentials() {
|
||||||
return basicAuthentication("user","INVALID");
|
return basicAuthenticationCredentials("user","INVALID");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@
|
|||||||
*/
|
*/
|
||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -27,11 +30,11 @@ import org.springframework.test.context.ActiveProfiles;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
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.ExchangeFilterFunction;
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
|
|
||||||
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.Credentials.basicAuthenticationCredentials;
|
||||||
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,6 +58,8 @@ public class HelloWebfluxFnApplicationTests {
|
|||||||
.bindToRouterFunction(this.routerFunction)
|
.bindToRouterFunction(this.routerFunction)
|
||||||
.webFilter(this.springSecurityFilterChain)
|
.webFilter(this.springSecurityFilterChain)
|
||||||
.apply(springSecurity())
|
.apply(springSecurity())
|
||||||
|
.configureClient()
|
||||||
|
.filter(basicAuthentication())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,11 +75,9 @@ public class HelloWebfluxFnApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenValidCredentialsThenOk() throws Exception {
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(userCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().json("{\"message\":\"Hello user!\"}");
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
||||||
@ -83,11 +86,9 @@ public class HelloWebfluxFnApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
.mutate()
|
|
||||||
.filter(invalidPassword())
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(invalidCredentials())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isUnauthorized()
|
.expectStatus().isUnauthorized()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
@ -110,11 +111,11 @@ public class HelloWebfluxFnApplicationTests {
|
|||||||
.expectStatus().isUnauthorized();
|
.expectStatus().isUnauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private Consumer<Map<String, Object>> userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthenticationCredentials("user","user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExchangeFilterFunction invalidPassword() {
|
private Consumer<Map<String, Object>> invalidCredentials() {
|
||||||
return basicAuthentication("user","INVALID");
|
return basicAuthenticationCredentials("user","INVALID");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ 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.basicAuthentication;
|
||||||
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,14 +103,13 @@ 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
|
||||||
.mutate()
|
|
||||||
.filter(basicAuthentication("test","this"))
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(basicAuthenticationCredentials("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"))
|
||||||
@ -125,14 +125,13 @@ 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
|
||||||
.mutate()
|
|
||||||
.filter(basicAuthentication("test", "this"))
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(basicAuthenticationCredentials("test", "this"))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isUnauthorized()
|
.expectStatus().isUnauthorized()
|
||||||
.expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"")
|
.expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"")
|
||||||
@ -212,14 +211,13 @@ 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
|
||||||
.mutate()
|
|
||||||
.filter(basicAuthentication("test","this"))
|
|
||||||
.build()
|
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
.attributes(basicAuthenticationCredentials("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"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user