|
|
|
|
@ -16,11 +16,6 @@
@@ -16,11 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.security.config.web.server |
|
|
|
|
|
|
|
|
|
import io.mockk.every |
|
|
|
|
import io.mockk.mockk |
|
|
|
|
import java.security.cert.Certificate |
|
|
|
|
import java.security.cert.CertificateFactory |
|
|
|
|
import java.security.cert.X509Certificate |
|
|
|
|
import org.junit.jupiter.api.Test |
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired |
|
|
|
|
@ -28,10 +23,6 @@ import org.springframework.context.ApplicationContext
@@ -28,10 +23,6 @@ import org.springframework.context.ApplicationContext
|
|
|
|
|
import org.springframework.context.annotation.Bean |
|
|
|
|
import org.springframework.context.annotation.Configuration |
|
|
|
|
import org.springframework.core.io.ClassPathResource |
|
|
|
|
import org.springframework.http.client.reactive.ClientHttpConnector |
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequestDecorator |
|
|
|
|
import org.springframework.http.server.reactive.SslInfo |
|
|
|
|
import org.springframework.lang.Nullable |
|
|
|
|
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity |
|
|
|
|
import org.springframework.security.config.test.SpringTestContext |
|
|
|
|
import org.springframework.security.config.test.SpringTestContextExtension |
|
|
|
|
@ -41,19 +32,15 @@ import org.springframework.security.core.userdetails.User
@@ -41,19 +32,15 @@ import org.springframework.security.core.userdetails.User
|
|
|
|
|
import org.springframework.security.web.authentication.preauth.x509.SubjectDnX509PrincipalExtractor |
|
|
|
|
import org.springframework.security.web.server.SecurityWebFilterChain |
|
|
|
|
import org.springframework.security.web.server.authentication.ReactivePreAuthenticatedAuthenticationManager |
|
|
|
|
import org.springframework.test.web.reactive.server.MockServerConfigurer |
|
|
|
|
import org.springframework.test.web.reactive.server.UserWebTestClientConfigurer.x509 |
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient |
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClientConfigurer |
|
|
|
|
import org.springframework.test.web.reactive.server.expectBody |
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping |
|
|
|
|
import org.springframework.web.bind.annotation.RestController |
|
|
|
|
import org.springframework.web.reactive.config.EnableWebFlux |
|
|
|
|
import org.springframework.web.server.ServerWebExchange |
|
|
|
|
import org.springframework.web.server.ServerWebExchangeDecorator |
|
|
|
|
import org.springframework.web.server.WebFilter |
|
|
|
|
import org.springframework.web.server.WebFilterChain |
|
|
|
|
import org.springframework.web.server.adapter.WebHttpHandlerBuilder |
|
|
|
|
import reactor.core.publisher.Mono |
|
|
|
|
import java.security.cert.Certificate |
|
|
|
|
import java.security.cert.CertificateFactory |
|
|
|
|
import java.security.cert.X509Certificate |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests for [ServerX509Dsl] |
|
|
|
|
@ -83,7 +70,7 @@ class ServerX509DslTests {
@@ -83,7 +70,7 @@ class ServerX509DslTests {
|
|
|
|
|
val certificate = loadCert<X509Certificate>("rod.cer") |
|
|
|
|
|
|
|
|
|
this.client |
|
|
|
|
.mutateWith(mockX509(certificate)) |
|
|
|
|
.mutateWith(x509(certificate)) |
|
|
|
|
.get() |
|
|
|
|
.uri("/username") |
|
|
|
|
.exchange() |
|
|
|
|
@ -111,7 +98,7 @@ class ServerX509DslTests {
@@ -111,7 +98,7 @@ class ServerX509DslTests {
|
|
|
|
|
val certificate = loadCert<X509Certificate>("rodatexampledotcom.cer") |
|
|
|
|
|
|
|
|
|
this.client |
|
|
|
|
.mutateWith(mockX509(certificate)) |
|
|
|
|
.mutateWith(x509(certificate)) |
|
|
|
|
.get() |
|
|
|
|
.uri("/username") |
|
|
|
|
.exchange() |
|
|
|
|
@ -143,7 +130,7 @@ class ServerX509DslTests {
@@ -143,7 +130,7 @@ class ServerX509DslTests {
|
|
|
|
|
val certificate = loadCert<X509Certificate>("rod.cer") |
|
|
|
|
|
|
|
|
|
this.client |
|
|
|
|
.mutateWith(mockX509(certificate)) |
|
|
|
|
.mutateWith(x509(certificate)) |
|
|
|
|
.get() |
|
|
|
|
.uri("/username") |
|
|
|
|
.exchange() |
|
|
|
|
@ -195,43 +182,6 @@ class ServerX509DslTests {
@@ -195,43 +182,6 @@ class ServerX509DslTests {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun mockX509(certificate: X509Certificate): X509Mutator { |
|
|
|
|
return X509Mutator(certificate) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private class X509Mutator internal constructor(private var certificate: X509Certificate) : WebTestClientConfigurer, MockServerConfigurer { |
|
|
|
|
|
|
|
|
|
override fun afterConfigurerAdded(builder: WebTestClient.Builder, |
|
|
|
|
@Nullable httpHandlerBuilder: WebHttpHandlerBuilder?, |
|
|
|
|
@Nullable connector: ClientHttpConnector?) { |
|
|
|
|
val filter = SetSslInfoWebFilter(certificate) |
|
|
|
|
httpHandlerBuilder!!.filters { filters: MutableList<WebFilter> -> filters.add(0, filter) } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private class SetSslInfoWebFilter(var certificate: X509Certificate) : WebFilter { |
|
|
|
|
|
|
|
|
|
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> { |
|
|
|
|
return chain.filter(decorate(exchange)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun decorate(exchange: ServerWebExchange): ServerWebExchange { |
|
|
|
|
val decorated: ServerHttpRequestDecorator = object : ServerHttpRequestDecorator(exchange.request) { |
|
|
|
|
override fun getSslInfo(): SslInfo { |
|
|
|
|
val sslInfo: SslInfo = mockk() |
|
|
|
|
every { sslInfo.sessionId } returns "sessionId" |
|
|
|
|
every { sslInfo.peerCertificates } returns arrayOf(certificate) |
|
|
|
|
return sslInfo |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return object : ServerWebExchangeDecorator(exchange) { |
|
|
|
|
override fun getRequest(): org.springframework.http.server.reactive.ServerHttpRequest { |
|
|
|
|
return decorated |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun <T : Certificate> loadCert(location: String): T { |
|
|
|
|
ClassPathResource(location).inputStream.use { inputStream -> |
|
|
|
|
val certFactory = CertificateFactory.getInstance("X.509") |
|
|
|
|
|