|
@@ -22,16 +22,16 @@ import org.springframework.beans.factory.annotation.Autowired
|
|
|
import org.springframework.context.ApplicationContext
|
|
|
import org.springframework.context.annotation.Bean
|
|
|
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
|
|
|
-import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
|
|
|
-import org.springframework.security.core.userdetails.User
|
|
|
import org.springframework.security.config.test.SpringTestContext
|
|
|
import org.springframework.security.config.test.SpringTestContextExtension
|
|
|
+import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
|
|
|
+import org.springframework.security.core.userdetails.User
|
|
|
import org.springframework.security.web.server.SecurityWebFilterChain
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient
|
|
|
import org.springframework.web.bind.annotation.RequestMapping
|
|
|
import org.springframework.web.bind.annotation.RestController
|
|
|
import org.springframework.web.reactive.config.EnableWebFlux
|
|
|
-import java.util.*
|
|
|
+import java.util.Base64
|
|
|
|
|
|
/**
|
|
|
* Tests for [AuthorizeExchangeDsl]
|
|
@@ -181,4 +181,40 @@ class AuthorizeExchangeDslTests {
|
|
|
return MapReactiveUserDetailsService(user)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ fun `request when ip address does not match then responds with forbidden`() {
|
|
|
+ this.spring.register(HasIpAddressConfig::class.java).autowire()
|
|
|
+
|
|
|
+ this.client
|
|
|
+ .get()
|
|
|
+ .uri("/")
|
|
|
+ .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:password".toByteArray()))
|
|
|
+ .exchange()
|
|
|
+ .expectStatus().isForbidden
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebFluxSecurity
|
|
|
+ @EnableWebFlux
|
|
|
+ open class HasIpAddressConfig {
|
|
|
+ @Bean
|
|
|
+ open fun springWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
|
|
+ return http {
|
|
|
+ authorizeExchange {
|
|
|
+ authorize(anyExchange, hasIpAddress("10.0.0.0/24"))
|
|
|
+ }
|
|
|
+ httpBasic { }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ open fun userDetailsService(): MapReactiveUserDetailsService {
|
|
|
+ val user = User.withDefaultPasswordEncoder()
|
|
|
+ .username("user")
|
|
|
+ .password("password")
|
|
|
+ .roles("USER")
|
|
|
+ .build()
|
|
|
+ return MapReactiveUserDetailsService(user)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|