Search in sources :

Example 6 with HttpServletRequestWrapper

use of jakarta.servlet.http.HttpServletRequestWrapper in project tomcat by apache.

the class Request method setRequest.

/**
 * Set a wrapped HttpServletRequest to pass to the application. Components
 * wishing to wrap the request should obtain the request via
 * {@link #getRequest()}, wrap it and then call this method with the
 * wrapped request.
 *
 * @param applicationRequest The wrapped request to pass to the application
 */
public void setRequest(HttpServletRequest applicationRequest) {
    // Check the wrapper wraps this request
    ServletRequest r = applicationRequest;
    while (r instanceof HttpServletRequestWrapper) {
        r = ((HttpServletRequestWrapper) r).getRequest();
    }
    if (r != facade) {
        throw new IllegalArgumentException(sm.getString("request.illegalWrap"));
    }
    this.applicationRequest = applicationRequest;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper)

Example 7 with HttpServletRequestWrapper

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

the class HttpSessionSecurityContextRepositoryTests method traverseWrappedRequests.

// SEC-2578
@Test
public void traverseWrappedRequests() {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
    SecurityContext context = repo.loadContext(holder);
    assertThat(request.getSession(false)).isNull();
    // Simulate authentication during the request
    context.setAuthentication(this.testToken);
    repo.saveContext(context, new HttpServletRequestWrapper(holder.getRequest()), new HttpServletResponseWrapper(holder.getResponse()));
    assertThat(request.getSession(false)).isNotNull();
    assertThat(request.getSession().getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY)).isEqualTo(context);
}
Also used : HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TransientSecurityContext(org.springframework.security.core.context.TransientSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 8 with HttpServletRequestWrapper

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

the class DebugFilterTests method doFilterDoesNotWrapWithDebugRequestWrapperAgain.

@Test
public void doFilterDoesNotWrapWithDebugRequestWrapperAgain() throws Exception {
    setupMocks();
    given(this.request.getAttribute(this.requestAttr)).willReturn(Boolean.TRUE);
    HttpServletRequest fireWalledRequest = new HttpServletRequestWrapper(new DebugRequestWrapper(this.request));
    this.filter.doFilter(fireWalledRequest, this.response, this.filterChain);
    verify(this.fcp).doFilter(fireWalledRequest, this.response, this.filterChain);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DebugRequestWrapper(org.springframework.security.web.debug.DebugFilter.DebugRequestWrapper) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) Test(org.junit.jupiter.api.Test)

Example 9 with HttpServletRequestWrapper

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

the class DefaultSavedRequestMixinTests method serializeDefaultRequestBuildWithConstructorTest.

@Test
public void serializeDefaultRequestBuildWithConstructorTest() throws IOException, JSONException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("x-auth-token", "12");
    // Spring 5 MockHttpServletRequest automatically adds a header when the cookies
    // are set. To get consistency we override the request.
    HttpServletRequest requestToWrite = new HttpServletRequestWrapper(request) {

        @Override
        public Cookie[] getCookies() {
            return new Cookie[] { new Cookie("SESSION", "123456789") };
        }
    };
    String actualString = this.mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new DefaultSavedRequest(requestToWrite, new PortResolverImpl()));
    JSONAssert.assertEquals(REQUEST_JSON, actualString, true);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SavedCookie(org.springframework.security.web.savedrequest.SavedCookie) Cookie(jakarta.servlet.http.Cookie) PortResolverImpl(org.springframework.security.web.PortResolverImpl) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequestWrapper (jakarta.servlet.http.HttpServletRequestWrapper)9 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7 Test (org.junit.jupiter.api.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 ServletException (jakarta.servlet.ServletException)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 Filter (jakarta.servlet.Filter)1 FilterChain (jakarta.servlet.FilterChain)1 ServletRequest (jakarta.servlet.ServletRequest)1 Cookie (jakarta.servlet.http.Cookie)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1