|
|
|
@ -18,6 +18,7 @@ package org.springframework.security.web.util.matcher; |
|
|
|
|
|
|
|
|
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.UnknownHostException; |
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
|
|
|
import java.util.Scanner; |
|
|
|
|
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
|
|
|
@ -47,7 +48,7 @@ public final class IpAddressMatcher implements RequestMatcher { |
|
|
|
* come. |
|
|
|
* come. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public IpAddressMatcher(String ipAddress) { |
|
|
|
public IpAddressMatcher(String ipAddress) { |
|
|
|
assertStartsWithHexa(ipAddress); |
|
|
|
assertNotHostName(ipAddress); |
|
|
|
if (ipAddress.indexOf('/') > 0) { |
|
|
|
if (ipAddress.indexOf('/') > 0) { |
|
|
|
String[] addressAndMask = StringUtils.split(ipAddress, "/"); |
|
|
|
String[] addressAndMask = StringUtils.split(ipAddress, "/"); |
|
|
|
ipAddress = addressAndMask[0]; |
|
|
|
ipAddress = addressAndMask[0]; |
|
|
|
@ -68,7 +69,7 @@ public final class IpAddressMatcher implements RequestMatcher { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean matches(String address) { |
|
|
|
public boolean matches(String address) { |
|
|
|
assertStartsWithHexa(address); |
|
|
|
assertNotHostName(address); |
|
|
|
InetAddress remoteAddress = parseAddress(address); |
|
|
|
InetAddress remoteAddress = parseAddress(address); |
|
|
|
if (!this.requiredAddress.getClass().equals(remoteAddress.getClass())) { |
|
|
|
if (!this.requiredAddress.getClass().equals(remoteAddress.getClass())) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
@ -91,11 +92,17 @@ public final class IpAddressMatcher implements RequestMatcher { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void assertStartsWithHexa(String ipAddress) { |
|
|
|
private void assertNotHostName(String ipAddress) { |
|
|
|
Assert.isTrue( |
|
|
|
String error = "ipAddress " + ipAddress + " doesn't look like an IP Address. Is it a host name?"; |
|
|
|
ipAddress.charAt(0) == '[' || ipAddress.charAt(0) == ':' |
|
|
|
Assert.isTrue(ipAddress.charAt(0) == '[' || ipAddress.charAt(0) == ':' |
|
|
|
|| Character.digit(ipAddress.charAt(0), 16) != -1, |
|
|
|
|| Character.digit(ipAddress.charAt(0), 16) != -1, error); |
|
|
|
"ipAddress must start with a [, :, or a hexadecimal digit"); |
|
|
|
if (!ipAddress.contains(":")) { |
|
|
|
|
|
|
|
Scanner parts = new Scanner(ipAddress); |
|
|
|
|
|
|
|
parts.useDelimiter("[./]"); |
|
|
|
|
|
|
|
while (parts.hasNext()) { |
|
|
|
|
|
|
|
Assert.isTrue(parts.hasNextInt() && parts.nextInt() >> 8 == 0, error); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private InetAddress parseAddress(String address) { |
|
|
|
private InetAddress parseAddress(String address) { |
|
|
|
|