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;
}
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);
}
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);
}
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);
}
}
Aggregations