类 AbstractHandshakeHandler
- java.lang.Object
- org.springframework.web.socket.server.support.AbstractHandshakeHandler
- 所有已实现的接口:
Lifecycle
,HandshakeHandler
- 直接已知子类:
DefaultHandshakeHandler
public abstract class AbstractHandshakeHandler extends Object implements HandshakeHandler, Lifecycle
A base class forHandshakeHandler
implementations, independent from the Servlet API.Performs initial validation of the WebSocket handshake request - possibly rejecting it through the appropriate HTTP status code - while also allowing its subclasses to override various parts of the negotiation process (e.g. origin validation, sub-protocol negotiation, extensions negotiation, etc).
If the negotiation succeeds, the actual upgrade is delegated to a server-specific
RequestUpgradeStrategy
, which will update the response as necessary and initialize the WebSocket. Currently supported servers are Jetty 9.0-9.3, Tomcat 7.0.47+ and 8.x, Undertow 1.0-1.3, GlassFish 4.1+, WebLogic 12.1.3+.- 从以下版本开始:
- 4.2
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
- 另请参阅:
JettyRequestUpgradeStrategy
,TomcatRequestUpgradeStrategy
,UndertowRequestUpgradeStrategy
,GlassFishRequestUpgradeStrategy
,WebLogicRequestUpgradeStrategy
构造器概要
构造器 限定符 构造器 说明 protected
AbstractHandshakeHandler()
Default constructor that auto-detects and instantiates aRequestUpgradeStrategy
suitable for the runtime container.protected
AbstractHandshakeHandler(RequestUpgradeStrategy requestUpgradeStrategy)
A constructor that accepts a runtime-specificRequestUpgradeStrategy
.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected List<String>
determineHandlerSupportedProtocols(WebSocketHandler handler)
Determine the sub-protocols supported by the given WebSocketHandler by checking whether it is an instance ofSubProtocolCapable
.protected Principal
determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String,Object> attributes)
A method that can be used to associate a user with the WebSocket session in the process of being established.boolean
doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String,Object> attributes)
Initiate the handshake.protected void
doStart()
protected void
doStop()
protected List<WebSocketExtension>
filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions)
Filter the list of requested WebSocket extensions.RequestUpgradeStrategy
getRequestUpgradeStrategy()
Return theRequestUpgradeStrategy
for WebSocket requests.String[]
getSupportedProtocols()
Return the list of supported sub-protocols.protected String[]
getSupportedVersions()
protected void
handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response)
protected void
handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response)
protected void
handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response)
boolean
isRunning()
Check whether this component is currently running.protected boolean
isValidOrigin(ServerHttpRequest request)
Return whether the requestOrigin
header value is valid or not.protected boolean
isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders)
protected String
selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler)
Perform the sub-protocol negotiation based on requested and supported sub-protocols.void
setSupportedProtocols(String... protocols)
Use this property to configure the list of supported sub-protocols.void
start()
Start this component.void
stop()
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
构造器详细资料
AbstractHandshakeHandler
protected AbstractHandshakeHandler()
Default constructor that auto-detects and instantiates aRequestUpgradeStrategy
suitable for the runtime container.- 抛出:
IllegalStateException
- if noRequestUpgradeStrategy
can be found.
AbstractHandshakeHandler
protected AbstractHandshakeHandler(RequestUpgradeStrategy requestUpgradeStrategy)
A constructor that accepts a runtime-specificRequestUpgradeStrategy
.- 参数:
requestUpgradeStrategy
- the upgrade strategy to use
方法详细资料
getRequestUpgradeStrategy
public RequestUpgradeStrategy getRequestUpgradeStrategy()
Return theRequestUpgradeStrategy
for WebSocket requests.
setSupportedProtocols
public void setSupportedProtocols(String... protocols)
Use this property to configure the list of supported sub-protocols. The first configured sub-protocol that matches a client-requested sub-protocol is accepted. If there are no matches the response will not contain a Sec-WebSocket-Protocol header.Note that if the WebSocketHandler passed in at runtime is an instance of
SubProtocolCapable
then there is not need to explicitly configure this property. That is certainly the case with the built-in STOMP over WebSocket support. Therefore this property should be configured explicitly only if the WebSocketHandler does not implementSubProtocolCapable
.
getSupportedProtocols
public String[] getSupportedProtocols()
Return the list of supported sub-protocols.
start
public void start()
从接口复制的说明:Lifecycle
Start this component.Should not throw an exception if the component is already running.
In the case of a container, this will propagate the start signal to all components that apply.
- 指定者:
start
在接口中Lifecycle
- 另请参阅:
SmartLifecycle.isAutoStartup()
doStart
protected void doStart()
stop
public void stop()
从接口复制的说明:Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycle
and itsstop(Runnable)
variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown,
Lifecycle
beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.Should not throw an exception if the component is not running (not started yet).
In the case of a container, this will propagate the stop signal to all components that apply.
- 指定者:
stop
在接口中Lifecycle
- 另请参阅:
SmartLifecycle.stop(Runnable)
,DisposableBean.destroy()
doStop
protected void doStop()
isRunning
public boolean isRunning()
从接口复制的说明:Lifecycle
Check whether this component is currently running.In the case of a container, this will return
true
only if all components that apply are currently running.
doHandshake
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String,Object> attributes) throws HandshakeFailureException
从接口复制的说明:HandshakeHandler
Initiate the handshake.- 指定者:
doHandshake
在接口中HandshakeHandler
- 参数:
request
- the current requestresponse
- the current responsewsHandler
- the handler to process WebSocket messages; seePerConnectionWebSocketHandler
for providing a handler with per-connection lifecycle.attributes
- attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.- 返回:
- whether the handshake negotiation was successful or not. In either case the response status, headers, and body will have been updated to reflect the result of the negotiation
- 抛出:
HandshakeFailureException
- thrown when handshake processing failed to complete due to an internal, unrecoverable error, i.e. a server error as opposed to a failure to successfully negotiate the handshake.
handleInvalidUpgradeHeader
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException
- 抛出:
IOException
handleInvalidConnectHeader
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException
- 抛出:
IOException
isWebSocketVersionSupported
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders)
getSupportedVersions
protected String[] getSupportedVersions()
handleWebSocketVersionNotSupported
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response)
isValidOrigin
protected boolean isValidOrigin(ServerHttpRequest request)
Return whether the requestOrigin
header value is valid or not. By default, all origins as considered as valid. Consider using anOriginHandshakeInterceptor
for filtering origins if needed.
selectProtocol
protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler)
Perform the sub-protocol negotiation based on requested and supported sub-protocols. For the list of supported sub-protocols, this method first checks if the target WebSocketHandler is aSubProtocolCapable
and then also checks if any sub-protocols have been explicitly configured withsetSupportedProtocols(String...)
.- 参数:
requestedProtocols
- the requested sub-protocolswebSocketHandler
- the WebSocketHandler that will be used- 返回:
- the selected protocols or
null
- 另请参阅:
determineHandlerSupportedProtocols(WebSocketHandler)
determineHandlerSupportedProtocols
protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler)
Determine the sub-protocols supported by the given WebSocketHandler by checking whether it is an instance ofSubProtocolCapable
.- 参数:
handler
- the handler to check- 返回:
- a list of supported protocols, or an empty list if none available
filterRequestedExtensions
protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions)
Filter the list of requested WebSocket extensions.As of 4.1, the default implementation of this method filters the list to leave only extensions that are both requested and supported.
- 参数:
request
- the current requestrequestedExtensions
- the list of extensions requested by the clientsupportedExtensions
- the list of extensions supported by the server- 返回:
- the selected extensions or an empty list
determineUser
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String,Object> attributes)
A method that can be used to associate a user with the WebSocket session in the process of being established. The default implementation callsServerHttpRequest.getPrincipal()
Subclasses can provide custom logic for associating a user with a session, for example for assigning a name to anonymous users (i.e. not fully authenticated).
- 参数:
request
- the handshake requestwsHandler
- the WebSocket handler that will handle messagesattributes
- handshake attributes to pass to the WebSocket session- 返回:
- the user for the WebSocket session, or
null
if not available