WebSocketConfigurer is the Spring interface you implement to register raw WebSocket handlers. Its single method, registerWebSocketHandlers(WebSocketHandlerRegistry), maps URL paths to WebSocketHandler implementations and lets you add handshake interceptors, allowed origins and a SockJS fallback. Combined with @EnableWebSocket, it is the standard way to expose a low-level WebSocket endpoint in Spring MVC or Spring Boot.
Use it when you want direct control over messages. If you prefer a higher-level, topic-based model with STOMP, use WebSocketMessageBrokerConfigurer instead.
WebSocketConfigurer lives in org.springframework.web.socket.config.annotation. Spring calls its method at startup, and the registry you receive turns each addHandler call into a handshake endpoint that upgrades HTTP requests to WebSocket connections. Without it, the spring-boot-starter-websocket dependency gives you the infrastructure but no endpoints.
You need the starter in your build:
In Maven the artifact is org.springframework.boot:spring-boot-starter-websocket. It uses the Jakarta WebSocket API, so on Spring Boot 3 and later the package names are jakarta.websocket, not javax.websocket.
Create a handler by extending TextWebSocketHandler (or BinaryWebSocketHandler), then register it.
A browser client connects with new WebSocket("wss://host/ws/chat"). The handler is a single shared instance, so keep per-connection state in the WebSocketSession or in a thread-safe map, not in plain fields.
The registration returned by addHandler is where most customisation happens.
| Option | Purpose |
|---|---|
addInterceptors | Run code before and after the handshake, for example to copy HTTP session or auth data into session attributes |
setAllowedOrigins / setAllowedOriginPatterns | Restrict which browser origins may connect |
withSockJS() | Provide a fallback for browsers or proxies without WebSocket support |
setHandshakeHandler | Customise the upgrade, such as choosing the user principal |
Spring allows only same-origin requests by default. Setting the allowed origin to "*" disables that protection, which exposes you to cross-site WebSocket hijacking, so list your real origins. Modern browsers all support WebSocket, so SockJS is rarely needed now.
Container limits for message size and idle timeouts are configured with a ServletServerContainerFactoryBean.
With Spring Security, the handshake is an ordinary HTTP request, so authenticate it with your normal filter chain and read the Principal from the WebSocketSession. Authentication happens once at handshake time; authorise each message yourself if different messages need different permissions.
Raw handlers require you to define your own message format, routing and broadcast logic. STOMP over WebSocket adds destinations, subscriptions and a broker.
| Need | Choose |
|---|---|
| Custom binary or text protocol, full control | WebSocketConfigurer |
Pub/sub topics, @MessageMapping, user queues | WebSocketMessageBrokerConfigurer with @EnableWebSocketMessageBroker |
If you only push one-way updates from the server, Server-Sent Events can also be simpler than WebSocket.
Yes, when you implement WebSocketConfigurer. Spring Boot does not enable raw handler registration by itself.
The origin check usually fails. Add your site's origin with setAllowedOrigins or setAllowedOriginPatterns.
Yes. Declare the handler as a bean and inject it into the configurer, instead of calling new inside the method.
Spring uses one handler instance for all sessions, and sending concurrently on one session is not safe. Wrap sessions with ConcurrentWebSocketSessionDecorator when several threads send.
WebSocketConfigurer is the registration point for low-level WebSocket handlers in Spring. Implement it with @EnableWebSocket, map handlers to paths, restrict origins, add handshake interceptors, and move to STOMP when you need topics and brokered messaging.