Search in sources :

Example 11 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-security by spring-projects.

the class LoginUrlAuthenticationEntryPoint method commence.

/**
 * Performs the redirect (or forward) to the login form URL.
 */
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
    if (!this.useForward) {
        // redirect to login page. Use https if forceHttps true
        String redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);
        this.redirectStrategy.sendRedirect(request, response, redirectUrl);
        return;
    }
    String redirectUrl = null;
    if (this.forceHttps && "http".equals(request.getScheme())) {
        // First redirect the current request to HTTPS. When that request is received,
        // the forward to the login page will be used.
        redirectUrl = buildHttpsRedirectUrlForRequest(request);
    }
    if (redirectUrl != null) {
        this.redirectStrategy.sendRedirect(request, response, redirectUrl);
        return;
    }
    String loginForm = determineUrlToUseForThisRequest(request, response, authException);
    logger.debug(LogMessage.format("Server side forward to: %s", loginForm));
    RequestDispatcher dispatcher = request.getRequestDispatcher(loginForm);
    dispatcher.forward(request, response);
    return;
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 12 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-security by spring-projects.

the class RequestWrapperTests method requestDispatcherNotWrappedAfterReset.

@Test
public void requestDispatcherNotWrappedAfterReset() {
    String path = "/forward/path";
    HttpServletRequest request = mock(HttpServletRequest.class);
    RequestDispatcher dispatcher = mock(RequestDispatcher.class);
    given(request.getRequestDispatcher(path)).willReturn(dispatcher);
    RequestWrapper wrapper = new RequestWrapper(request);
    wrapper.reset();
    assertThat(wrapper.getRequestDispatcher(path)).isSameAs(dispatcher);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestDispatcher(jakarta.servlet.RequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 13 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-security by spring-projects.

the class RequestWrapperTests method resetWhenForward.

@Test
public void resetWhenForward() throws Exception {
    String denormalizedPath = testPaths.keySet().iterator().next();
    String forwardPath = "/forward/path";
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    RequestDispatcher mockDispatcher = mock(RequestDispatcher.class);
    given(mockRequest.getServletPath()).willReturn("");
    given(mockRequest.getPathInfo()).willReturn(denormalizedPath);
    given(mockRequest.getRequestDispatcher(forwardPath)).willReturn(mockDispatcher);
    RequestWrapper wrapper = new RequestWrapper(mockRequest);
    RequestDispatcher dispatcher = wrapper.getRequestDispatcher(forwardPath);
    dispatcher.forward(mockRequest, mockResponse);
    verify(mockRequest).getRequestDispatcher(forwardPath);
    verify(mockDispatcher).forward(mockRequest, mockResponse);
    assertThat(wrapper.getPathInfo()).isEqualTo(denormalizedPath);
    verify(mockRequest, times(2)).getPathInfo();
    // validate wrapper.getServletPath() delegates to the mock
    wrapper.getServletPath();
    verify(mockRequest, times(2)).getServletPath();
    verifyNoMoreInteractions(mockRequest, mockResponse, mockDispatcher);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) RequestDispatcher(jakarta.servlet.RequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 14 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-framework by spring-projects.

the class ControllerTests method doTestServletForwardingController.

private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletContext context = mock(ServletContext.class);
    RequestDispatcher dispatcher = mock(RequestDispatcher.class);
    given(request.getMethod()).willReturn("GET");
    given(context.getNamedDispatcher("action")).willReturn(dispatcher);
    if (include) {
        given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath");
    } else {
        given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    }
    StaticWebApplicationContext sac = new StaticWebApplicationContext();
    sac.setServletContext(context);
    sfc.setApplicationContext(sac);
    assertThat(sfc.handleRequest(request, response)).isNull();
    if (include) {
        verify(dispatcher).include(request, response);
    } else {
        verify(dispatcher).forward(request, response);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletContext(jakarta.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (jakarta.servlet.RequestDispatcher)14 ServletContext (jakarta.servlet.ServletContext)4 ServletException (jakarta.servlet.ServletException)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 Filter (jakarta.servlet.Filter)1 ServletRequest (jakarta.servlet.ServletRequest)1 ServletResponse (jakarta.servlet.ServletResponse)1 BodyContent (jakarta.servlet.jsp.tagext.BodyContent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AsyncDispatcher (org.apache.catalina.AsyncDispatcher)1 Session (org.apache.catalina.Session)1 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1