@@ -22,16 +22,16 @@ import org.springframework.beans.factory.annotation.Autowired
22
22
import org.springframework.context.ApplicationContext
23
23
import org.springframework.context.annotation.Bean
24
24
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
25
- import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
26
- import org.springframework.security.core.userdetails.User
27
25
import org.springframework.security.config.test.SpringTestContext
28
26
import org.springframework.security.config.test.SpringTestContextExtension
27
+ import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
28
+ import org.springframework.security.core.userdetails.User
29
29
import org.springframework.security.web.server.SecurityWebFilterChain
30
30
import org.springframework.test.web.reactive.server.WebTestClient
31
31
import org.springframework.web.bind.annotation.RequestMapping
32
32
import org.springframework.web.bind.annotation.RestController
33
33
import org.springframework.web.reactive.config.EnableWebFlux
34
- import java.util.*
34
+ import java.util.Base64
35
35
36
36
/* *
37
37
* Tests for [AuthorizeExchangeDsl]
@@ -181,4 +181,40 @@ class AuthorizeExchangeDslTests {
181
181
return MapReactiveUserDetailsService (user)
182
182
}
183
183
}
184
+
185
+ @Test
186
+ fun `request when ip address does not match then responds with forbidden` () {
187
+ this .spring.register(HasIpAddressConfig ::class .java).autowire()
188
+
189
+ this .client
190
+ .get()
191
+ .uri(" /" )
192
+ .header(" Authorization" , " Basic " + Base64 .getEncoder().encodeToString(" user:password" .toByteArray()))
193
+ .exchange()
194
+ .expectStatus().isForbidden
195
+ }
196
+
197
+ @EnableWebFluxSecurity
198
+ @EnableWebFlux
199
+ open class HasIpAddressConfig {
200
+ @Bean
201
+ open fun springWebFilterChain (http : ServerHttpSecurity ): SecurityWebFilterChain {
202
+ return http {
203
+ authorizeExchange {
204
+ authorize(anyExchange, hasIpAddress(" 10.0.0.0/24" ))
205
+ }
206
+ httpBasic { }
207
+ }
208
+ }
209
+
210
+ @Bean
211
+ open fun userDetailsService (): MapReactiveUserDetailsService {
212
+ val user = User .withDefaultPasswordEncoder()
213
+ .username(" user" )
214
+ .password(" password" )
215
+ .roles(" USER" )
216
+ .build()
217
+ return MapReactiveUserDetailsService (user)
218
+ }
219
+ }
184
220
}
0 commit comments