Skip to content

Commit ea7c720

Browse files
brunodmartinsjzheaux
authored andcommitted
Add hasIpAddress to Kotlin DSL
Closes gh-10577
1 parent ee14ba2 commit ea7c720

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

config/src/main/kotlin/org/springframework/security/config/annotation/web/AuthorizeHttpRequestsDsl.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.springframework.security.config.annotation.web.configurers.AuthorizeH
2626
import org.springframework.security.core.Authentication
2727
import org.springframework.security.web.access.intercept.AuthorizationFilter
2828
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
29+
import org.springframework.security.web.server.authorization.IpAddressAuthorizationManager
2930
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher
3031
import org.springframework.security.web.util.matcher.AnyRequestMatcher
3132
import org.springframework.security.web.util.matcher.RequestMatcher
@@ -222,6 +223,13 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
222223
return AuthorityAuthorizationManager.hasAnyRole(*roles)
223224
}
224225

226+
/**
227+
* Require a specific IP or range of IP addresses.
228+
* @since 6.3
229+
*/
230+
fun hasIpAddress(ipAddress: String): AuthorizationManager<RequestAuthorizationContext> =
231+
IpAddressAuthorizationManager.hasIpAddress(ipAddress)
232+
225233
/**
226234
* Specify that URLs are allowed by anyone.
227235
*/

config/src/test/kotlin/org/springframework/security/config/annotation/web/AuthorizeHttpRequestsDslTests.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,4 +816,41 @@ class AuthorizeHttpRequestsDslTests {
816816
}
817817

818818
}
819+
820+
@Test
821+
fun `request when ip address does not match then responds with forbidden`() {
822+
this.spring.register(HasIpAddressConfig::class.java).autowire()
823+
824+
this.mockMvc.perform(get("/path")
825+
.with { request ->
826+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error")
827+
request.apply {
828+
dispatcherType = DispatcherType.ERROR
829+
}
830+
})
831+
.andExpect(status().isForbidden)
832+
}
833+
834+
@Configuration
835+
@EnableWebSecurity
836+
@EnableWebMvc
837+
open class HasIpAddressConfig {
838+
839+
@Bean
840+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
841+
http {
842+
authorizeHttpRequests {
843+
authorize(anyRequest, hasIpAddress("10.0.0.0/24"))
844+
}
845+
}
846+
return http.build()
847+
}
848+
849+
@RestController
850+
internal class PathController {
851+
@RequestMapping("/path")
852+
fun path() {
853+
}
854+
}
855+
}
819856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authorization;
18+
19+
import java.util.function.Supplier;
20+
21+
import org.springframework.security.authorization.AuthorizationDecision;
22+
import org.springframework.security.authorization.AuthorizationManager;
23+
import org.springframework.security.core.Authentication;
24+
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
25+
import org.springframework.security.web.util.matcher.IpAddressMatcher;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* A {@link AuthorizationManager}, that determines if the current request contains the
30+
* specified address or range of addresses
31+
*
32+
* @author brunodmartins
33+
* @since 6.3
34+
*/
35+
public final class IpAddressAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {
36+
37+
private final IpAddressMatcher ipAddressMatcher;
38+
39+
IpAddressAuthorizationManager(String ipAddress) {
40+
this.ipAddressMatcher = new IpAddressMatcher(ipAddress);
41+
}
42+
43+
/**
44+
* Creates an instance of {@link IpAddressAuthorizationManager} with the provided IP
45+
* address.
46+
* @param ipAddress the address or range of addresses from which the request must
47+
* @return the new instance
48+
*/
49+
public static IpAddressAuthorizationManager hasIpAddress(String ipAddress) {
50+
Assert.notNull(ipAddress, "ipAddress cannot be null");
51+
return new IpAddressAuthorizationManager(ipAddress);
52+
}
53+
54+
@Override
55+
public AuthorizationDecision check(Supplier<Authentication> authentication,
56+
RequestAuthorizationContext requestAuthorizationContext) {
57+
return new AuthorizationDecision(
58+
this.ipAddressMatcher.matcher(requestAuthorizationContext.getRequest()).isMatch());
59+
}
60+
61+
}

0 commit comments

Comments
 (0)