Search in sources :

Example 1 with WebSecurityExpressionRoot

use of org.springframework.security.web.access.expression.WebSecurityExpressionRoot in project spring-security by spring-projects.

the class WebSecurityExpressionRootTests method ipAddressMatchesForEqualIpAddresses.

@Test
public void ipAddressMatchesForEqualIpAddresses() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/test");
    // IPv4
    request.setRemoteAddr("192.168.1.1");
    WebSecurityExpressionRoot root = new WebSecurityExpressionRoot(mock(Authentication.class), new FilterInvocation(request, mock(HttpServletResponse.class), mock(FilterChain.class)));
    assertThat(root.hasIpAddress("192.168.1.1")).isTrue();
    // IPv6 Address
    request.setRemoteAddr("fa:db8:85a3::8a2e:370:7334");
    assertThat(root.hasIpAddress("fa:db8:85a3::8a2e:370:7334")).isTrue();
}
Also used : WebSecurityExpressionRoot(org.springframework.security.web.access.expression.WebSecurityExpressionRoot) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) FilterInvocation(org.springframework.security.web.FilterInvocation) Test(org.junit.Test)

Example 2 with WebSecurityExpressionRoot

use of org.springframework.security.web.access.expression.WebSecurityExpressionRoot in project spring-security by spring-projects.

the class WebSecurityExpressionRootTests method addressesInIpRangeMatch.

@Test
public void addressesInIpRangeMatch() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/test");
    WebSecurityExpressionRoot root = new WebSecurityExpressionRoot(mock(Authentication.class), new FilterInvocation(request, mock(HttpServletResponse.class), mock(FilterChain.class)));
    for (int i = 0; i < 255; i++) {
        request.setRemoteAddr("192.168.1." + i);
        assertThat(root.hasIpAddress("192.168.1.0/24")).isTrue();
    }
    request.setRemoteAddr("192.168.1.127");
    // 25 = FF FF FF 80
    assertThat(root.hasIpAddress("192.168.1.0/25")).isTrue();
    // encroach on the mask
    request.setRemoteAddr("192.168.1.128");
    assertThat(root.hasIpAddress("192.168.1.0/25")).isFalse();
    request.setRemoteAddr("192.168.1.255");
    assertThat(root.hasIpAddress("192.168.1.128/25")).isTrue();
    assertThat(root.hasIpAddress("192.168.1.192/26")).isTrue();
    assertThat(root.hasIpAddress("192.168.1.224/27")).isTrue();
    assertThat(root.hasIpAddress("192.168.1.240/27")).isTrue();
    assertThat(root.hasIpAddress("192.168.1.255/32")).isTrue();
    request.setRemoteAddr("202.24.199.127");
    assertThat(root.hasIpAddress("202.24.0.0/14")).isTrue();
    request.setRemoteAddr("202.25.179.135");
    assertThat(root.hasIpAddress("202.24.0.0/14")).isTrue();
    request.setRemoteAddr("202.26.179.135");
    assertThat(root.hasIpAddress("202.24.0.0/14")).isTrue();
}
Also used : WebSecurityExpressionRoot(org.springframework.security.web.access.expression.WebSecurityExpressionRoot) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) FilterInvocation(org.springframework.security.web.FilterInvocation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 Authentication (org.springframework.security.core.Authentication)2 FilterInvocation (org.springframework.security.web.FilterInvocation)2 WebSecurityExpressionRoot (org.springframework.security.web.access.expression.WebSecurityExpressionRoot)2