Top Description Fields Constructors Methods
org.springframework.web.socket.server.support

public Class OriginHandshakeInterceptor

extends Object
implements HandshakeInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.socket.server.HandshakeInterceptor
Imports
java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List, .Map, org.apache.commons.logging.Log, .LogFactory, org.springframework.http.HttpStatus, org.springframework.http.server.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, org.springframework.web.cors.CorsConfiguration, org.springframework.web.socket.WebSocketHandler, org.springframework.web.socket.server.HandshakeInterceptor, org.springframework.web.util.WebUtils

An interceptor to check request Origin header value against a collection of allowed origins.
Author
Sebastien Deleuze
Since
4.1.2

Field Summary

Modifier and TypeField and Description
private final CorsConfiguration
protected final Log

Constructor Summary

AccessConstructor and Description
public
OriginHandshakeInterceptor()

Default constructor with only same origin requests allowed.

public
OriginHandshakeInterceptor(Collection<String> allowedOrigins)

Constructor using the specified allowed origin values.

Method Summary

Modifier and TypeMethod and Description
public void
afterHandshake(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the target WebSocket handler
wsHandler
,
Exception
an exception raised during the handshake, or null if none
exception
)

Implements org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Invoked after the handshake is done.

public boolean
beforeHandshake(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the target WebSocket handler
wsHandler
,
Map<String, Object>
the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
attributes
)

Implements org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Invoked before the handshake is processed.

public Collection<String>
getAllowedOriginPatterns()

Return the configured allowed origin patterns.

public Collection<String>
getAllowedOrigins()

Return the configured allowed origins.

public void
setAllowedOriginPatterns(Collection<String> allowedOriginPatterns)

Alternative to setAllowedOrigins(Collection) that supports more flexible patterns for specifying the origins for which cross-origin requests are allowed from a browser.

public void
setAllowedOrigins(Collection<String> allowedOrigins)

Set the origins for which cross-origin requests are allowed from a browser.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

corsConfigurationback to summary
private final CorsConfiguration corsConfiguration
loggerback to summary
protected final Log logger

Constructor Detail

OriginHandshakeInterceptorback to summary
public OriginHandshakeInterceptor()

Default constructor with only same origin requests allowed.

OriginHandshakeInterceptorback to summary
public OriginHandshakeInterceptor(Collection<String> allowedOrigins)

Constructor using the specified allowed origin values.

See Also
setAllowedOrigins(Collection)

Method Detail

afterHandshakeback to summary
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception)

Implements org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Doc from org.springframework.web.socket.server.HandshakeInterceptor.afterHandshake.

Invoked after the handshake is done. The response status and headers indicate the results of the handshake, i.e. whether it was successful or not.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

wsHandler:WebSocketHandler

the target WebSocket handler

exception:Exception

an exception raised during the handshake, or null if none

Annotations
@Override
beforeHandshakeback to summary
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception

Implements org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Doc from org.springframework.web.socket.server.HandshakeInterceptor.beforeHandshake.

Invoked before the handshake is processed.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

wsHandler:WebSocketHandler

the target WebSocket handler

attributes:Map<String, Object>

the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.

Returns:boolean

whether to proceed with the handshake (true) or abort (false)

Annotations
@Override
getAllowedOriginPatternsback to summary
public Collection<String> getAllowedOriginPatterns()

Return the configured allowed origin patterns.

Since
5.3.2
getAllowedOriginsback to summary
public Collection<String> getAllowedOrigins()

Return the configured allowed origins.

Since
4.1.5
setAllowedOriginPatternsback to summary
public void setAllowedOriginPatterns(Collection<String> allowedOriginPatterns)

Alternative to setAllowedOrigins(Collection) that supports more flexible patterns for specifying the origins for which cross-origin requests are allowed from a browser. Please, refer to CorsConfiguration#setAllowedOriginPatterns(List) for format details and other considerations.

By default this is not set.

Since
5.3.2
setAllowedOriginsback to summary
public void setAllowedOrigins(Collection<String> allowedOrigins)

Set the origins for which cross-origin requests are allowed from a browser. Please, refer to CorsConfiguration#setAllowedOrigins(List) for format details and considerations, and keep in mind that the CORS spec does not allow use of "*" with allowCredentials=true. For more flexible origin patterns use setAllowedOriginPatterns instead.

By default, no origins are allowed. When allowedOriginPatterns is also set, then that takes precedence over this property.

Note when SockJS is enabled and origins are restricted, transport types that do not allow to check request origin (Iframe based transports) are disabled. As a consequence, IE 6 to 9 are not supported when origins are restricted.

See Also
setAllowedOriginPatterns(Collection), RFC 6454: The Web Origin Concept