SEC-1033: Add basic equality support for hasIpAddress() expression.
This commit is contained in:
parent
3da68a7a82
commit
7767a9ed60
|
@ -1,7 +1,11 @@
|
|||
package org.springframework.security.expression.support;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.intercept.web.FilterInvocation;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -16,4 +20,34 @@ class WebSecurityExpressionRoot extends SecurityExpressionRoot {
|
|||
super(a);
|
||||
this.filterInvocation = fi;
|
||||
}
|
||||
|
||||
public boolean hasIpAddress(String ipAddress) {
|
||||
byte[] mask = null;
|
||||
|
||||
if (ipAddress.indexOf('/') > 0) {
|
||||
String[] addressAndMask = StringUtils.split(ipAddress, "/");
|
||||
ipAddress = addressAndMask[0];
|
||||
try {
|
||||
mask = InetAddress.getByName(addressAndMask[1]).getAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new IllegalArgumentException("Failed to parse mask" + addressAndMask[1], e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
InetAddress requiredAddress = InetAddress.getByName(ipAddress);
|
||||
InetAddress remoteAddress = InetAddress.getByName(filterInvocation.getHttpRequest().getRemoteAddr());
|
||||
|
||||
if (mask == null) {
|
||||
return remoteAddress.equals(requiredAddress);
|
||||
} else {
|
||||
|
||||
}
|
||||
// byte[] remoteAddress = InetAddress.getByName(filterInvocation.getHttpRequest().getRemoteAddr()).getAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new IllegalArgumentException("Failed to parse " + ipAddress, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package org.springframework.security.expression.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.jmock.Mockery;
|
||||
import org.jmock.integration.junit4.JUnit4Mockery;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.intercept.web.FilterInvocation;
|
||||
import org.springframework.security.util.FilterInvocationUtils;
|
||||
|
||||
public class WebSecurityExpressionRootTests {
|
||||
Mockery jmock = new JUnit4Mockery();
|
||||
|
||||
@Test
|
||||
public void ipAddressMatchesForEqualIpAddresses() throws Exception {
|
||||
FilterInvocation fi = FilterInvocationUtils.create("/test");
|
||||
MockHttpServletRequest request = (MockHttpServletRequest) fi.getHttpRequest();
|
||||
request.setRemoteAddr("192.168.1.1");
|
||||
WebSecurityExpressionRoot root = new WebSecurityExpressionRoot(jmock.mock(Authentication.class), fi);
|
||||
|
||||
assertTrue(root.hasIpAddress("192.168.1.1"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue