Search in sources :

Example 6 with PortMapperImpl

use of org.springframework.security.web.PortMapperImpl in project spring-security by spring-projects.

the class RetryWithHttpEntryPointTests method testNormalOperationWithNullQueryString.

@Test
public void testNormalOperationWithNullQueryString() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bigWebApp/hello");
    request.setScheme("https");
    request.setServerName("www.example.com");
    request.setServerPort(443);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(80, 443));
    ep.commence(request, response);
    assertThat(response.getRedirectedUrl()).isEqualTo("http://www.example.com/bigWebApp/hello");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with PortMapperImpl

use of org.springframework.security.web.PortMapperImpl in project spring-security by spring-projects.

the class RetryWithHttpEntryPointTests method testOperationWhenTargetPortIsUnknown.

@Test
public void testOperationWhenTargetPortIsUnknown() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bigWebApp");
    request.setQueryString("open=true");
    request.setScheme("https");
    request.setServerName("www.example.com");
    request.setServerPort(8768);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(8768, 1234));
    ep.commence(request, response);
    assertThat(response.getRedirectedUrl()).isEqualTo("/bigWebApp?open=true");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with PortMapperImpl

use of org.springframework.security.web.PortMapperImpl in project spring-security by spring-projects.

the class LoginUrlAuthenticationEntryPointTests method testNormalOperation.

@Test
public void testNormalOperation() throws Exception {
    LoginUrlAuthenticationEntryPoint ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(80, 443));
    ep.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/some_path");
    request.setContextPath("/bigWebApp");
    request.setScheme("http");
    request.setServerName("www.example.com");
    request.setContextPath("/bigWebApp");
    request.setServerPort(80);
    MockHttpServletResponse response = new MockHttpServletResponse();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("http://www.example.com/bigWebApp/hello");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 9 with PortMapperImpl

use of org.springframework.security.web.PortMapperImpl in project spring-security by spring-projects.

the class LoginUrlAuthenticationEntryPointTests method testHttpsOperationFromOriginalHttpUrl.

@Test
public void testHttpsOperationFromOriginalHttpUrl() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/some_path");
    request.setScheme("http");
    request.setServerName("www.example.com");
    request.setContextPath("/bigWebApp");
    request.setServerPort(80);
    MockHttpServletResponse response = new MockHttpServletResponse();
    LoginUrlAuthenticationEntryPoint ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setForceHttps(true);
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(80, 443));
    ep.afterPropertiesSet();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com/bigWebApp/hello");
    request.setServerPort(8080);
    response = new MockHttpServletResponse();
    ep.setPortResolver(new MockPortResolver(8080, 8443));
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:8443/bigWebApp/hello");
    // Now test an unusual custom HTTP:HTTPS is handled properly
    request.setServerPort(8888);
    response = new MockHttpServletResponse();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:8443/bigWebApp/hello");
    PortMapperImpl portMapper = new PortMapperImpl();
    Map<String, String> map = new HashMap<String, String>();
    map.put("8888", "9999");
    portMapper.setPortMappings(map);
    response = new MockHttpServletResponse();
    ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setForceHttps(true);
    ep.setPortMapper(portMapper);
    ep.setPortResolver(new MockPortResolver(8888, 9999));
    ep.afterPropertiesSet();
    ep.commence(request, response, null);
    assertThat(response.getRedirectedUrl()).isEqualTo("https://www.example.com:9999/bigWebApp/hello");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with PortMapperImpl

use of org.springframework.security.web.PortMapperImpl in project spring-security by spring-projects.

the class PortMapperConfigurer method getPortMapper.

/**
	 * Gets the {@link PortMapper} to use. If {@link #portMapper(PortMapper)} was not
	 * invoked, builds a {@link PortMapperImpl} using the port mappings specified with
	 * {@link #http(int)}.
	 *
	 * @return the {@link PortMapper} to use
	 */
private PortMapper getPortMapper() {
    if (portMapper == null) {
        PortMapperImpl portMapper = new PortMapperImpl();
        portMapper.setPortMappings(httpsPortMappings);
        this.portMapper = portMapper;
    }
    return portMapper;
}
Also used : PortMapperImpl(org.springframework.security.web.PortMapperImpl)

Aggregations

PortMapperImpl (org.springframework.security.web.PortMapperImpl)14 Test (org.junit.Test)13 MockPortResolver (org.springframework.security.MockPortResolver)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)11 RetryWithHttpsEntryPoint (org.springframework.security.web.access.channel.RetryWithHttpsEntryPoint)5 HashMap (java.util.HashMap)2