CorsUtils.java 3.7 KB
Newer Older
S
Sebastien Deleuze 已提交
1
/*
S
Sebastien Deleuze 已提交
2
 * Copyright 2002-2019 the original author or authors.
S
Sebastien Deleuze 已提交
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
S
Sebastien Deleuze 已提交
9 10 11 12 13 14 15 16 17 18
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.cors.reactive;

19 20
import java.net.URI;

S
Sebastien Deleuze 已提交
21 22 23
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
J
Juergen Hoeller 已提交
24
import org.springframework.lang.Nullable;
S
Sebastien Deleuze 已提交
25 26 27 28 29 30
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * Utility class for CORS reactive request handling based on the
S
Sam Brannen 已提交
31
 * <a href="https://www.w3.org/TR/cors/">CORS W3C recommendation</a>.
S
Sebastien Deleuze 已提交
32 33 34 35 36 37 38
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
public abstract class CorsUtils {

	/**
S
Sebastien Deleuze 已提交
39 40
	 * Returns {@code true} if the request is a valid CORS one by checking {@code Origin}
	 * header presence and ensuring that origins are different via {@link #isSameOrigin}.
S
Sebastien Deleuze 已提交
41
	 */
S
Sebastien Deleuze 已提交
42
	@SuppressWarnings("deprecation")
S
Sebastien Deleuze 已提交
43
	public static boolean isCorsRequest(ServerHttpRequest request) {
S
Sebastien Deleuze 已提交
44
		return request.getHeaders().containsKey(HttpHeaders.ORIGIN) && !isSameOrigin(request);
S
Sebastien Deleuze 已提交
45 46 47 48
	}

	/**
	 * Returns {@code true} if the request is a valid CORS pre-flight one.
S
Sebastien Deleuze 已提交
49 50
	 * To be used in combination with {@link #isCorsRequest(ServerHttpRequest)} since
	 * regular CORS checks are not invoked here for performance reasons.
S
Sebastien Deleuze 已提交
51 52
	 */
	public static boolean isPreFlightRequest(ServerHttpRequest request) {
S
Sebastien Deleuze 已提交
53
		return (request.getMethod() == HttpMethod.OPTIONS && request.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD));
S
Sebastien Deleuze 已提交
54 55 56
	}

	/**
57 58 59 60 61 62 63 64
	 * 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.
	 *
S
Sebastien Deleuze 已提交
65
	 * @return {@code true} if the request is a same-origin one, {@code false} in case
66
	 * of a cross-origin request
S
Sebastien Deleuze 已提交
67
	 * @deprecated as of 5.2, same-origin checks are performed directly by {@link #isCorsRequest}
S
Sebastien Deleuze 已提交
68
	 */
S
Sebastien Deleuze 已提交
69
	@Deprecated
S
Sebastien Deleuze 已提交
70 71 72 73 74
	public static boolean isSameOrigin(ServerHttpRequest request) {
		String origin = request.getHeaders().getOrigin();
		if (origin == null) {
			return true;
		}
J
Juergen Hoeller 已提交
75

76
		URI uri = request.getURI();
77
		String actualScheme = uri.getScheme();
78 79
		String actualHost = uri.getHost();
		int actualPort = getPort(uri.getScheme(), uri.getPort());
80
		Assert.notNull(actualScheme, "Actual request scheme must not be null");
S
Sebastien Deleuze 已提交
81 82
		Assert.notNull(actualHost, "Actual request host must not be null");
		Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
J
Juergen Hoeller 已提交
83

S
Sebastien Deleuze 已提交
84
		UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
85 86
		return (actualScheme.equals(originUrl.getScheme()) &&
				actualHost.equals(originUrl.getHost()) &&
87
				actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));
S
Sebastien Deleuze 已提交
88 89
	}

J
Juergen Hoeller 已提交
90
	private static int getPort(@Nullable String scheme, int port) {
S
Sebastien Deleuze 已提交
91
		if (port == -1) {
S
sdeleuze 已提交
92
			if ("http".equals(scheme) || "ws".equals(scheme)) {
S
Sebastien Deleuze 已提交
93 94
				port = 80;
			}
S
sdeleuze 已提交
95
			else if ("https".equals(scheme) || "wss".equals(scheme)) {
S
Sebastien Deleuze 已提交
96 97 98 99 100 101 102
				port = 443;
			}
		}
		return port;
	}

}