OriginHandshakeInterceptor.java 3.7 KB
Newer Older
1
/*
S
Sebastien Deleuze 已提交
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.ArrayList;
import java.util.Collection;
S
Sebastien Deleuze 已提交
21
import java.util.Collections;
22 23 24 25 26 27 28 29 30
import java.util.List;
import java.util.Map;

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;
S
Sebastien Deleuze 已提交
31
import org.springframework.util.Assert;
32 33
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
34
import org.springframework.web.util.WebUtils;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * An interceptor to check request {@code Origin} header value against a collection of
 * allowed origins.
 *
 * @author Sebastien Deleuze
 * @since 4.1.2
 */
public class OriginHandshakeInterceptor implements HandshakeInterceptor {

	protected Log logger = LogFactory.getLog(getClass());

	private final List<String> allowedOrigins;


	/**
51
	 * Default constructor with only same origin requests allowed.
52 53 54 55 56
	 */
	public OriginHandshakeInterceptor() {
		this.allowedOrigins = new ArrayList<String>();
	}

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

67
	/**
S
Sebastien Deleuze 已提交
68 69 70 71 72 73 74 75
	 * Configure allowed {@code Origin} header values. This check is mostly designed for
	 * browser clients. There is nothing preventing other types of client to modify the
	 * {@code Origin} header value.
	 *
	 * <p>Each provided allowed origin must start by "http://", "https://" or be "*"
	 * (means that all origins are allowed).
	 *
	 * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
76 77
	 */
	public void setAllowedOrigins(Collection<String> allowedOrigins) {
S
Sebastien Deleuze 已提交
78 79 80 81 82
		Assert.notNull(allowedOrigins, "Allowed origin Collection must not be null");
		for (String allowedOrigin : allowedOrigins) {
			Assert.isTrue(allowedOrigin.equals("*") || allowedOrigin.startsWith("http://") ||
					allowedOrigin.startsWith("https://"), "Invalid allowed origin provided: \"" +
					allowedOrigin + "\". It must start with \"http://\", \"https://\" or be \"*\"");
83
		}
S
Sebastien Deleuze 已提交
84 85 86 87 88 89 90 91 92 93
		this.allowedOrigins.clear();
		this.allowedOrigins.addAll(allowedOrigins);
	}

	/**
	 * @see #setAllowedOrigins(Collection)
	 * @since 4.1.5
	 */
	public Collection<String> getAllowedOrigins() {
		return Collections.unmodifiableList(this.allowedOrigins);
94 95 96 97 98
	}

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
99
		if (!WebUtils.isValidOrigin(request, this.allowedOrigins)) {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
			response.setStatusCode(HttpStatus.FORBIDDEN);
			if (logger.isDebugEnabled()) {
				logger.debug("Handshake request rejected, Origin header value "
						+ request.getHeaders().getOrigin() + " not allowed");
			}
			return false;
		}
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, Exception exception) {
	}

}