CorsUtils.java 3.0 KB
Newer Older
S
Sebastien Deleuze 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*
 * Copyright 2002-2015 the original author or authors.
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
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
 * <a href="http://www.w3.org/TR/cors/">CORS W3C recommendation</a>.
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
public abstract class CorsUtils {

	/**
	 * Returns {@code true} if the request is a valid CORS one.
	 */
	public static boolean isCorsRequest(ServerHttpRequest request) {
		return (request.getHeaders().get(HttpHeaders.ORIGIN) != null);
	}

	/**
	 * Returns {@code true} if the request is a valid CORS pre-flight one.
	 */
	public static boolean isPreFlightRequest(ServerHttpRequest request) {
		return (isCorsRequest(request) && HttpMethod.OPTIONS == request.getMethod() &&
				request.getHeaders().get(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null);
	}

	/**
	 * Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
S
sdeleuze 已提交
54 55
	 * {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and
	 * @code X-Forwarded-Port} headers.
S
Sebastien Deleuze 已提交
56 57 58 59 60 61 62 63 64 65 66
	 * @return {@code true} if the request is a same-origin one, {@code false} in case
	 * of cross-origin request.
	 */
	public static boolean isSameOrigin(ServerHttpRequest request) {
		String origin = request.getHeaders().getOrigin();
		if (origin == null) {
			return true;
		}
		UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpRequest(request);
		UriComponents actualUrl = urlBuilder.build();
		String actualHost = actualUrl.getHost();
S
sdeleuze 已提交
67
		int actualPort = getPort(actualUrl.getScheme(), actualUrl.getPort());
S
Sebastien Deleuze 已提交
68 69 70
		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();
S
sdeleuze 已提交
71
		return (actualHost.equals(originUrl.getHost()) && actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));
S
Sebastien Deleuze 已提交
72 73
	}

S
sdeleuze 已提交
74
	private static int getPort(String scheme, int port) {
S
Sebastien Deleuze 已提交
75
		if (port == -1) {
S
sdeleuze 已提交
76
			if ("http".equals(scheme) || "ws".equals(scheme)) {
S
Sebastien Deleuze 已提交
77 78
				port = 80;
			}
S
sdeleuze 已提交
79
			else if ("https".equals(scheme) || "wss".equals(scheme)) {
S
Sebastien Deleuze 已提交
80 81 82 83 84 85 86
				port = 443;
			}
		}
		return port;
	}

}