OriginHandshakeInterceptor.java 3.6 KB
Newer Older
1
/*
P
Phillip Webb 已提交
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.socket.server.support;

import java.util.Collection;
S
Sebastien Deleuze 已提交
20
import java.util.Collections;
21
import java.util.LinkedHashSet;
22
import java.util.Map;
23
import java.util.Set;
24 25 26 27 28 29 30

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
31
import org.springframework.lang.Nullable;
S
Sebastien Deleuze 已提交
32
import org.springframework.util.Assert;
33 34
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
35
import org.springframework.web.util.WebUtils;
36 37

/**
38 39
 * An interceptor to check request {@code Origin} header value against a
 * collection of allowed origins.
40 41 42 43 44 45
 *
 * @author Sebastien Deleuze
 * @since 4.1.2
 */
public class OriginHandshakeInterceptor implements HandshakeInterceptor {

J
Juergen Hoeller 已提交
46
	protected final Log logger = LogFactory.getLog(getClass());
47

48
	private final Set<String> allowedOrigins = new LinkedHashSet<>();
49 50 51


	/**
52
	 * Default constructor with only same origin requests allowed.
53 54 55 56
	 */
	public OriginHandshakeInterceptor() {
	}

57 58 59 60 61 62 63 64
	/**
	 * Constructor using the specified allowed origin values.
	 * @see #setAllowedOrigins(Collection)
	 */
	public OriginHandshakeInterceptor(Collection<String> allowedOrigins) {
		setAllowedOrigins(allowedOrigins);
	}

65

66
	/**
67 68 69 70
	 * Configure allowed {@code Origin} header values. This check is mostly
	 * designed for browsers. There is nothing preventing other types of client
	 * to modify the {@code Origin} header value.
	 * <p>Each provided allowed origin must have a scheme, and optionally a port
S
Spring Operator 已提交
71
	 * (e.g. "https://example.org", "https://example.org:9090"). An allowed origin
72
	 * string may also be "*" in which case all origins are allowed.
S
Sebastien Deleuze 已提交
73
	 * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
74 75
	 */
	public void setAllowedOrigins(Collection<String> allowedOrigins) {
76
		Assert.notNull(allowedOrigins, "Allowed origins Collection must not be null");
S
Sebastien Deleuze 已提交
77 78 79 80 81
		this.allowedOrigins.clear();
		this.allowedOrigins.addAll(allowedOrigins);
	}

	/**
P
Phillip Webb 已提交
82
	 * Return the allowed {@code Origin} header values.
S
Sebastien Deleuze 已提交
83
	 * @since 4.1.5
84
	 * @see #setAllowedOrigins
S
Sebastien Deleuze 已提交
85 86
	 */
	public Collection<String> getAllowedOrigins() {
87
		return Collections.unmodifiableSet(this.allowedOrigins);
88 89
	}

90

91 92 93
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
94

95
		if (!WebUtils.isSameOrigin(request) && !WebUtils.isValidOrigin(request, this.allowedOrigins)) {
96 97
			response.setStatusCode(HttpStatus.FORBIDDEN);
			if (logger.isDebugEnabled()) {
98 99
				logger.debug("Handshake request rejected, Origin header value " +
						request.getHeaders().getOrigin() + " not allowed");
100 101 102 103 104 105 106 107
			}
			return false;
		}
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
108
			WebSocketHandler wsHandler, @Nullable Exception exception) {
109 110 111
	}

}