Search in sources :

Example 61 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class CorsUtils method isSameOrigin.

/**
 * Check if the request is a same-origin one, based on {@code Origin}, and
 * {@code Host} headers.
 *
 * <p><strong>Note:</strong> as of 5.1 this method ignores
 * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
 * client-originated address. Consider using the {@code ForwardedHeaderFilter}
 * to extract and use, or to discard such headers.
 * @return {@code true} if the request is a same-origin one, {@code false} in case
 * of a cross-origin request
 * @deprecated as of 5.2, same-origin checks are performed directly by {@link #isCorsRequest}
 */
@Deprecated
public static boolean isSameOrigin(ServerHttpRequest request) {
    String origin = request.getHeaders().getOrigin();
    if (origin == null) {
        return true;
    }
    URI uri = request.getURI();
    String actualScheme = uri.getScheme();
    String actualHost = uri.getHost();
    int actualPort = getPort(uri.getScheme(), uri.getPort());
    Assert.notNull(actualScheme, "Actual request scheme must not be null");
    Assert.notNull(actualHost, "Actual request host must not be null");
    Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
    UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
    return (actualScheme.equals(originUrl.getScheme()) && actualHost.equals(originUrl.getHost()) && actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) URI(java.net.URI)

Example 62 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class RequestContextUtils method saveOutputFlashMap.

/**
 * Convenience method that retrieves the {@link #getOutputFlashMap "output"
 * FlashMap}, updates it with the path and query params of the target URL,
 * and then saves it using the {@link #getFlashMapManager FlashMapManager}.
 * @param location the target URL for the redirect
 * @param request the current request
 * @param response the current response
 * @since 5.0
 */
public static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response) {
    FlashMap flashMap = getOutputFlashMap(request);
    if (CollectionUtils.isEmpty(flashMap)) {
        return;
    }
    UriComponents uriComponents = UriComponentsBuilder.fromUriString(location).build();
    flashMap.setTargetRequestPath(uriComponents.getPath());
    flashMap.addTargetRequestParams(uriComponents.getQueryParams());
    FlashMapManager manager = getFlashMapManager(request);
    Assert.state(manager != null, "No FlashMapManager. Is this a DispatcherServlet handled request?");
    manager.saveOutputFlashMap(flashMap, request, response);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) UriComponents(org.springframework.web.util.UriComponents) FlashMapManager(org.springframework.web.servlet.FlashMapManager)

Example 63 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method usesFirstHostOfXForwardedHost.

@Test
public void usesFirstHostOfXForwardedHost() throws Exception {
    this.request.setScheme("https");
    this.request.addHeader("X-Forwarded-Host", "barfoo:8888, localhost:8088");
    adaptRequestFromForwardedHeaders();
    UriComponents uriComponents = fromController(PersonControllerImpl.class).build();
    assertThat(uriComponents.toUriString()).startsWith("https://barfoo:8888");
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.jupiter.api.Test)

Example 64 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method fromMethodCallWithCustomBaseUrlViaStaticCall.

@Test
public void fromMethodCallWithCustomBaseUrlViaStaticCall() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
    UriComponents uriComponents = fromMethodCall(builder, on(ControllerWithMethods.class).myMethod(null)).build();
    assertThat(uriComponents.toString()).isEqualTo("https://example.org:9090/base/something/else");
    assertThat(builder.toUriString()).isEqualTo("https://example.org:9090/base");
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Test(org.junit.jupiter.api.Test)

Example 65 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method fromMethodNameWithOptionalParam.

// SPR-14405
@Test
public void fromMethodNameWithOptionalParam() {
    UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithOptionalParam", new Object[] { null }).build();
    assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/optional-param");
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.jupiter.api.Test)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)133 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)26 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HashMap (java.util.HashMap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)4 MethodParameter (org.springframework.core.MethodParameter)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2