Код: Выделить всё
localhost:4200
При попытке создать Stomp-клиент и запуская соединение, я получаю следующую консольную ошибку:
Код: Выделить всё
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Компонент Angular 2
Код: Выделить всё
connect() {
var socket = new SockJS('http://localhost:8081/chat');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, function(result) {
console.log('Connected: ' + result);
this.stompClient.subscribe('/topic/messages', function(message) {
console.log(message);
});
});
}
Код: Выделить всё
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("http://localhost:4200").withSockJS();
}
}
Код: Выделить всё
{"entropy":-1720701276,"origins":["*:*"],"cookie_needed":true,"websocket":true}
Код: Выделить всё
public class MessageController {
@MessageMapping("/chat")
@SendTo("/topic/messages")
public Message send(Message message) throws Exception {
return new Message(message.getFrom(), message.getText());
}
}
Код: Выделить всё
public class SecurityConfig extends Auth0SecurityConfig {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();
}
}
После дополнительного тестирования и исследования выяснилось, что проблема только в происходит с использованием Chrome. Проблема может быть связана с: https://github.com/sockjs/sockjs-node/issues/177
ОБНОВЛЕНИЕ
Я создал CORSFilter, как упоминалось в chsdk, и использовал метод addFilterBefore(): https://stackoverflow.com/a/40300363/4836952.
Код: Выделить всё
@Bean
CORSFilter corsFilter() {
CORSFilter filter = new CORSFilter();
return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(corsFilter(), SessionManagementFilter.class).authorizeRequests().anyRequest().permitAll();
http.csrf().disable();
}

Подробнее здесь: https://stackoverflow.com/questions/431 ... -credentia