HandshakeInterceptor is a Spring WebSocket interface that lets you run code before and after the HTTP upgrade request that opens a WebSocket connection. In beforeHandshake you can inspect the request, authenticate the caller, copy data into the session attributes, or reject the connection by returning false. In afterHandshake you can log the outcome or clean up. It lives in org.springframework.web.socket.server.
This matters because a WebSocket starts as a normal HTTP GET with an Upgrade header, and that request is the only moment when headers, cookies, query parameters and the HTTP session are easy to read.
HttpSessionHandshakeInterceptorA WebSocket connection is created in two steps. First the client sends an HTTP request asking to upgrade. Spring's HandshakeHandler completes the upgrade and creates a WebSocketSession. After that, messages flow over the open socket and no HTTP request context is available.
HandshakeInterceptor hooks into the first step:
beforeHandshake runs, in registration order. If one returns false, the handshake is aborted.WebSocketSession is created, with the attributes map you filled in.afterHandshake runs, with an exception if the handshake failed.The interface has two methods:
Everything you put into the attributes map is available later through WebSocketSession.getAttributes(), and with STOMP through the message headers' session attributes.
The example below reads a token from a query parameter, validates it, and stores the user ID in the session attributes. It rejects the handshake with a 401 when the token is missing or invalid.
Browsers cannot set custom headers such as Authorization on a WebSocket opened with new WebSocket(url). That is why tokens are often sent in a query parameter or a cookie. A query token appears in server and proxy logs, so use short-lived, single-use tokens, or rely on a session cookie with Spring Security.
For a plain WebSocket handler, add the interceptor through WebSocketConfigurer.
For STOMP over WebSocket, register it on the endpoint instead:
In both cases setAllowedOriginPatterns restricts which web origins may connect. Leaving it open ("*") is acceptable only for public, unauthenticated feeds.
Once the handshake succeeds, the handler reads the stored values.
HttpSessionHandshakeInterceptorSpring ships HttpSessionHandshakeInterceptor, which copies HTTP session attributes into the WebSocket session attributes. It is useful when the user already logged in through a normal web page and the app uses server-side sessions.
With Spring Security, the authenticated Principal is already taken from the handshake request, so for simple cases you may not need a custom interceptor at all. For STOMP messaging, Spring Security's message authorization or a ChannelInterceptor is the better place to check permissions on each message.
| Need | Use |
|---|---|
| Check a token or cookie at connect time | HandshakeInterceptor |
| Copy HTTP session data to the socket | HttpSessionHandshakeInterceptor |
| Authorise each STOMP message or subscription | ChannelInterceptor or Spring Security messaging rules |
Choose the Principal for the session | A custom DefaultHandshakeHandler.determineUser |
| Rate-limit connects | HandshakeInterceptor, or the reverse proxy |
beforeHandshake returns false?The handshake stops and no WebSocket is created. Set a status code on the response first, such as 401 or 403, so the client sees why.
Yes, through request.getHeaders(). Remember that browser JavaScript cannot set custom headers on a WebSocket, only cookies and the URL.
No. It runs once per connection. Use a ChannelInterceptor for per-message logic with STOMP.
No. WebFlux uses its own WebSocketHandler and HandshakeInfo types. The interface described here is for Spring MVC (servlet) applications.
HandshakeInterceptor runs around the HTTP upgrade of a WebSocket connection, which is the one place to authenticate the client, restrict origins and pass data into the session attributes. Register it with addInterceptors, set allowed origins, and use a ChannelInterceptor for per-message checks.