Search in sources :

Example 11 with MockPortResolver

use of org.springframework.security.MockPortResolver 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 12 with MockPortResolver

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

the class DefaultSavedRequestTests method headersAreCaseInsensitive.

// SEC-308, SEC-315
@Test
public void headersAreCaseInsensitive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("USER-aGenT", "Mozilla");
    DefaultSavedRequest saved = new DefaultSavedRequest(request, new MockPortResolver(8080, 8443));
    assertThat(saved.getHeaderValues("user-agent").get(0)).isEqualTo("Mozilla");
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.Test)

Example 13 with MockPortResolver

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

the class DefaultSavedRequestTests method parametersAreCaseSensitive.

// SEC-3082
@Test
public void parametersAreCaseSensitive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("AnotHerTest", "Hi dad");
    request.addParameter("thisisatest", "Hi mom");
    DefaultSavedRequest saved = new DefaultSavedRequest(request, new MockPortResolver(8080, 8443));
    assertThat(saved.getParameterValues("thisisatest")[0]).isEqualTo("Hi mom");
    assertThat(saved.getParameterValues("anothertest")).isNull();
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.Test)

Example 14 with MockPortResolver

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

the class ExceptionTranslationFilterTests method redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhenAuthenticationException.

@Test
public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhenAuthenticationException() throws Exception {
    // Setup our HTTP request
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/secure/page.html");
    request.setServerPort(8080);
    request.setScheme("http");
    request.setServerName("www.example.com");
    request.setContextPath("/mycontext");
    request.setRequestURI("/mycontext/secure/page.html");
    // Setup the FilterChain to thrown an authentication failure exception
    FilterChain fc = mock(FilterChain.class);
    doThrow(new BadCredentialsException("")).when(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
    // Test
    HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
    ExceptionTranslationFilter filter = new ExceptionTranslationFilter(mockEntryPoint, requestCache);
    requestCache.setPortResolver(new MockPortResolver(8080, 8443));
    filter.afterPropertiesSet();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/login.jsp");
    assertThat(getSavedRequestUrl(request)).isEqualTo("http://www.example.com:8080/mycontext/secure/page.html");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockPortResolver(org.springframework.security.MockPortResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpSessionRequestCache(org.springframework.security.web.savedrequest.HttpSessionRequestCache) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 15 with MockPortResolver

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

the class LoginUrlAuthenticationEntryPointTests method testGettersSetters.

@Test
public void testGettersSetters() {
    LoginUrlAuthenticationEntryPoint ep = new LoginUrlAuthenticationEntryPoint("/hello");
    ep.setPortMapper(new PortMapperImpl());
    ep.setPortResolver(new MockPortResolver(8080, 8443));
    assertThat(ep.getLoginFormUrl()).isEqualTo("/hello");
    assertThat(ep.getPortMapper() != null).isTrue();
    assertThat(ep.getPortResolver() != null).isTrue();
    ep.setForceHttps(false);
    assertThat(ep.isForceHttps()).isFalse();
    ep.setForceHttps(true);
    assertThat(ep.isForceHttps()).isTrue();
    assertThat(ep.isUseForward()).isFalse();
    ep.setUseForward(true);
    assertThat(ep.isUseForward()).isTrue();
}
Also used : MockPortResolver(org.springframework.security.MockPortResolver) PortMapperImpl(org.springframework.security.web.PortMapperImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 MockPortResolver (org.springframework.security.MockPortResolver)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 PortMapperImpl (org.springframework.security.web.PortMapperImpl)13 RetryWithHttpsEntryPoint (org.springframework.security.web.access.channel.RetryWithHttpsEntryPoint)5 DefaultSavedRequest (org.springframework.security.web.savedrequest.DefaultSavedRequest)3 HashMap (java.util.HashMap)2 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 HttpSessionRequestCache (org.springframework.security.web.savedrequest.HttpSessionRequestCache)1