Spring Security has no class literally named WebSocketSecurityConfigurer. The role people usually mean is the configuration that secures WebSocket and STOMP messaging. In current Spring Security (6.x with Spring Boot 3), you do that by annotating a configuration class with @EnableWebSocketSecurity and declaring an AuthorizationManager<Message<?>> bean. Older code extended AbstractSecurityWebSocketMessageBrokerConfigurer, which was the previous way to do the same job.
Securing WebSockets has two parts: protecting the HTTP handshake that upgrades the connection, and authorizing each STOMP message after the connection is open. This article shows both.
A WebSocket starts as an ordinary HTTP request with an Upgrade header. After the upgrade, the HTTP security filter chain no longer sees individual messages. Spring Security therefore protects WebSockets in two layers:
| Layer | What it protects | Mechanism |
|---|---|---|
| Handshake | Who may open a connection | SecurityFilterChain rules on the endpoint URL |
| Messages | Who may send to or subscribe to which destination | AuthorizationManager<Message<?>> through @EnableWebSocketSecurity |
If you use raw WebSockets with a custom WebSocketHandler, only the handshake layer applies and you check permissions yourself inside the handler. Message-level rules apply to STOMP over WebSocket, where messages have destinations such as /app/chat.
Treat the endpoint like any other URL. The authenticated principal from the handshake request becomes the session principal:
Browsers cannot set custom headers such as Authorization on the WebSocket constructor. For token-based apps, either rely on a session cookie, pass a short-lived ticket as a query parameter obtained from an authenticated REST call, or send the token in the STOMP CONNECT frame headers and authenticate it with a channel interceptor.
Add @EnableWebSocketSecurity and publish a AuthorizationManager<Message<?>> built with MessageMatcherDelegatingAuthorizationManager.Builder:
Rules are evaluated in order and the first match wins, so put specific rules first. The final anyMessage().denyAll() gives a default-deny policy, which is the safe choice because any destination you forget is blocked.
simpDestMatchers matches messages sent from the client to the application (prefix /app in the usual setup). simpSubscribeDestMatchers matches subscriptions to broker destinations such as /topic/**. Matchers use Ant-style patterns on the destination.
You can also protect handlers with annotations. With @EnableWebSocketSecurity active, method security applies to @MessageMapping methods:
The Principal parameter is the authenticated user, and convertAndSendToUser routes replies to that user's /user/queue/... destinations only.
By default, Spring Security requires a CSRF token on the STOMP CONNECT message, which stops cross-site WebSocket hijacking. A browser client reads the token and sends it in a header on connect. If you use pure token authentication without cookies, you can disable the CSRF check for the socket by publishing a custom ChannelInterceptor setup, but keep the origin check: configure setAllowedOrigins with explicit origins instead of "*" in your WebSocketMessageBrokerConfigurer.
Code written for Spring Security 5.x extended AbstractSecurityWebSocketMessageBrokerConfigurer and overrode configureInbound(MessageSecurityMetadataSourceRegistry). The same rules (nullDestMatcher, simpDestMatchers, anyMessage) map to the new builder. Move the rules into the AuthorizationManager<Message<?>> bean and replace the old base class with @EnableWebSocketSecurity.
Not as a public Spring Security API. Use @EnableWebSocketSecurity, or on old versions AbstractSecurityWebSocketMessageBrokerConfigurer.
No. It protects the handshake only. Messages after the upgrade need message-level rules or checks in your handler.
The STOMP CONNECT frame must carry the CSRF token. Pass it from the page in the connect headers, or adjust the CSRF configuration deliberately.
Send it in the STOMP CONNECT header and validate it in a ChannelInterceptor, because the browser WebSocket API cannot set an Authorization header.
WebSocket security in Spring has two layers: the handshake, protected by your SecurityFilterChain, and the messages, protected by an AuthorizationManager<Message<?>> enabled through @EnableWebSocketSecurity. End the message rules with anyMessage().denyAll(), keep CSRF and origin checks on, and migrate old AbstractSecurityWebSocketMessageBrokerConfigurer code to the new builder.