WebFlux HTTP Basic & Form Login Sessions
By default both HTTP Basic and form log are enabled. Now HTTP Session will not be used for HTTP Basic, but will be for form log in.
This commit is contained in:
parent
9133eb1b78
commit
0a36359f11
|
@ -28,7 +28,6 @@ import org.springframework.security.config.web.server.HttpSecurity;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsRepository;
|
import org.springframework.security.core.userdetails.UserDetailsRepository;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver;
|
import org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver;
|
||||||
import org.springframework.security.web.server.context.WebSessionSecurityContextRepository;
|
|
||||||
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
||||||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
||||||
|
|
||||||
|
@ -69,7 +68,6 @@ public class HttpSecurityConfiguration implements WebFluxConfigurer {
|
||||||
public HttpSecurity httpSecurity() {
|
public HttpSecurity httpSecurity() {
|
||||||
return http()
|
return http()
|
||||||
.authenticationManager(authenticationManager())
|
.authenticationManager(authenticationManager())
|
||||||
.securityContextRepository(new WebSessionSecurityContextRepository())
|
|
||||||
.headers().and()
|
.headers().and()
|
||||||
.httpBasic().and()
|
.httpBasic().and()
|
||||||
.formLogin().and();
|
.formLogin().and();
|
||||||
|
|
|
@ -206,16 +206,9 @@ public class HttpSecurity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private SecurityContextRepositoryWebFilter securityContextRepositoryWebFilter() {
|
private SecurityContextRepositoryWebFilter securityContextRepositoryWebFilter() {
|
||||||
SecurityContextRepository respository = getSecurityContextRepository();
|
SecurityContextRepository repository = this.securityContextRepository;
|
||||||
return respository == null ? null :
|
return repository == null ? null :
|
||||||
new SecurityContextRepositoryWebFilter(respository);
|
new SecurityContextRepositoryWebFilter(repository);
|
||||||
}
|
|
||||||
|
|
||||||
private SecurityContextRepository getSecurityContextRepository() {
|
|
||||||
if(this.securityContextRepository == null && this.formLogin != null) {
|
|
||||||
this.securityContextRepository = this.formLogin.securityContextRepository;
|
|
||||||
}
|
|
||||||
return this.securityContextRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpSecurity() {}
|
private HttpSecurity() {}
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||||
import org.springframework.security.web.server.WebFilterChainFilter;
|
import org.springframework.security.web.server.WebFilterChainFilter;
|
||||||
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
|
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.reactive.server.FluxExchangeResult;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
@ -75,6 +76,21 @@ public class EnableWebFluxSecurityTests {
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void authenticateWhenBasicThenNoSession() {
|
||||||
|
WebTestClient client = WebTestClientBuilder
|
||||||
|
.bindToWebFilters(this.springSecurityFilterChain)
|
||||||
|
.filter(basicAuthentication())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
FluxExchangeResult<String> result = client.get()
|
||||||
|
.attributes(basicAuthenticationCredentials("user", "password")).exchange()
|
||||||
|
.expectStatus()
|
||||||
|
.isOk()
|
||||||
|
.returnResult(String.class);
|
||||||
|
result.assertWithDiagnostics(() -> assertThat(result.getResponseCookies().isEmpty()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultPopulatesReactorContext() {
|
public void defaultPopulatesReactorContext() {
|
||||||
Principal currentPrincipal = new TestingAuthenticationToken("user", "password", "ROLE_USER");
|
Principal currentPrincipal = new TestingAuthenticationToken("user", "password", "ROLE_USER");
|
||||||
|
|
|
@ -22,11 +22,9 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.ResponseCookie;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
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.ExchangeResult;
|
|
||||||
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.client.ExchangeFilterFunction;
|
||||||
|
|
||||||
|
@ -89,28 +87,6 @@ public class HelloWebfluxApplicationITests {
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void sessionWorks() throws Exception {
|
|
||||||
ExchangeResult result = this.rest
|
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.returnResult(String.class);
|
|
||||||
|
|
||||||
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
|
|
||||||
|
|
||||||
this.rest
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.cookie(session.getName(), session.getValue())
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private ExchangeFilterFunction userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthentication("user","user");
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,9 @@ import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.http.ResponseCookie;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
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.ExchangeResult;
|
|
||||||
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.client.ExchangeFilterFunction;
|
||||||
|
|
||||||
|
@ -91,28 +89,6 @@ public class HelloWebfluxApplicationTests {
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void sessionWorks() throws Exception {
|
|
||||||
ExchangeResult result = this.rest
|
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.returnResult(String.class);
|
|
||||||
|
|
||||||
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
|
|
||||||
|
|
||||||
this.rest
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.cookie(session.getName(), session.getValue())
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
|
|
|
@ -22,11 +22,9 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.ResponseCookie;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
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.ExchangeResult;
|
|
||||||
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.client.ExchangeFilterFunction;
|
||||||
|
|
||||||
|
@ -88,28 +86,6 @@ public class HelloWebfluxFnApplicationITests {
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void sessionWorks() throws Exception {
|
|
||||||
ExchangeResult result = this.rest
|
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.returnResult(String.class);
|
|
||||||
|
|
||||||
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
|
|
||||||
|
|
||||||
this.rest
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.cookie(session.getName(), session.getValue())
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExchangeFilterFunction userCredentials() {
|
private ExchangeFilterFunction userCredentials() {
|
||||||
return basicAuthentication("user","user");
|
return basicAuthentication("user","user");
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,10 @@ 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.http.ResponseCookie;
|
|
||||||
import org.springframework.security.web.server.WebFilterChainFilter;
|
import org.springframework.security.web.server.WebFilterChainFilter;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
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.ExchangeResult;
|
|
||||||
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.client.ExchangeFilterFunction;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
|
@ -95,28 +93,6 @@ public class HelloWebfluxFnApplicationTests {
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void sessionWorks() throws Exception {
|
|
||||||
ExchangeResult result = this.rest
|
|
||||||
.mutate()
|
|
||||||
.filter(userCredentials())
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.returnResult(String.class);
|
|
||||||
|
|
||||||
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
|
|
||||||
|
|
||||||
this.rest
|
|
||||||
.get()
|
|
||||||
.uri("/")
|
|
||||||
.cookie(session.getName(), session.getValue())
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
||||||
this.rest
|
this.rest
|
||||||
|
|
Loading…
Reference in New Issue