Spring does not use classic CORS headers for WebSocket. Browsers do not enforce CORS on WebSocket connections at all; instead, the server inspects the Origin header during the HTTP upgrade handshake and rejects it when it is not allowed. In Spring you configure this with setAllowedOriginPatterns(...) (or setAllowedOrigins(...)) on the WebSocket or STOMP endpoint registration.
By default Spring accepts only same-origin handshakes, so a front end served from another domain or port is refused with a 403 until you list its origin. This guide covers raw WebSocket handlers, STOMP endpoints, SockJS, credentials and Spring Security.
A browser opening new WebSocket("wss://api.example.com/ws") from https://app.example.com sends an HTTP GET with Upgrade: websocket and an Origin: https://app.example.com header. There is no preflight request and no Access-Control-Allow-Origin response. Spring's OriginHandshakeInterceptor compares the Origin value with the allowed list and answers 403 Forbidden on a mismatch.
This check matters for security. Without it, any web page a user visits could open an authenticated socket to your server using the user's cookies (cross-site WebSocket hijacking). Allow only the origins you control.
Non-browser clients such as curl or mobile apps usually send no Origin header, and Spring lets those through, so origin checks are not a substitute for authentication.
Implement WebSocketConfigurer and set the allowed origins on the handler registration.
setAllowedOriginPatterns accepts wildcards in the host and port, for example https://*.example.com or http://localhost:*. The older setAllowedOrigins takes exact values only, and "*" there means any origin.
For STOMP over WebSocket, set the origins on the endpoint registration in a WebSocketMessageBrokerConfigurer.
The browser client then connects with new WebSocket("wss://api.example.com/ws") or a STOMP library pointed at the same URL.
If you use SockJS, add .withSockJS() after the origins.
SockJS also makes regular HTTP requests (for /info and for its transport fallbacks), and the same allowed origins list drives its CORS headers. SockJS is rarely needed today because every current browser supports native WebSocket.
Cookies are sent with a WebSocket handshake to the target site regardless of CORS, which is why the origin check is the main defence. Rules to follow:
"*" on an endpoint that authenticates by cookie.setAllowedOriginPatterns rather than setAllowedOrigins("*"), because patterns let you restrict to a family of hosts.When Spring Security is present, the handshake is an ordinary HTTP request, so it must pass the filter chain: permit the endpoint or require authentication, and make sure your CORS and CSRF settings do not block it. A global CorsConfigurationSource or @CrossOrigin applies to HTTP controllers, not to the WebSocket origin check, so the allowed list must be set on the WebSocket registration itself. Prefer authenticating STOMP messages with a token in the CONNECT frame and a ChannelInterceptor rather than relying on cookies.
The Origin header is not on the allowed list. Add the exact scheme, host and port of your front end to setAllowedOriginPatterns.
No. It configures Spring MVC controller endpoints. WebSocket origins are set on the handler or STOMP endpoint registration.
Only for public, unauthenticated feeds. If the socket uses cookies or user sessions, allowing all origins exposes it to cross-site WebSocket hijacking.
setAllowedOrigins matches exact strings, while setAllowedOriginPatterns supports wildcards such as https://*.example.com. Prefer patterns.
WebSocket "CORS" in Spring is an origin check during the handshake. Register allowed origins on the handler or STOMP endpoint with setAllowedOriginPatterns, avoid "*" for authenticated sockets, and secure the handshake and messages with Spring Security and token authentication.