I'm working on a server application which uses both REST endpoints and a SockJS websocket. This used to work fine under Spring 5.2 and below.
However, since the 5.3 release, the following method exists within org.springframework.web.cors.CorsConfiguration
:
public void validateAllowCredentials() {
if (this.allowCredentials == Boolean.TRUE &&
this.allowedOrigins != null && this.allowedOrigins.contains(ALL)) {
throw new IllegalArgumentException(
"When allowCredentials is true, allowedOrigins cannot contain the special value \"*\"" +
"since that cannot be set on the \"Access-Control-Allow-Origin\" response header. " +
"To allow credentials to a set of origins, list them explicitly " +
"or consider using \"allowedOriginPatterns\" instead.");
}
}
So far, my socket was configured like this:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
// prefix for the client to send messages to the server
config.setApplicationDestinationPrefixes("/app");
// prefix for the client to receive broadcasted messages from the server
config.enableSimpleBroker("/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// defines the url of the socket so the client can connect to it
registry.addEndpoint("/socketendpoint").setAllowedOrigins("*").withSockJS();
}
}
Now I'm facing a real issue:
- If I keep the
setAllowedOrigins("*")
in theWebSocketConfiguration
, then I will face the error thrown invalidateAllowCredentials
. - If I remove the
setAllowedOrigins("*")
, then the SockJS clients will recieve anError during WebSocket handshake: Unexpected response code: 403
.
I don't know the origin domain at compile time.
I already tried a Cors Filter and a Cors Configuration that use the typical "return the origin
header you find in the request as allow-origin
" pattern that is usually used to circumvent the allow-origin: "*"
, but some SockJS requests don't have an origin
header assigned...
How do I fix this?