Witam. Mam aplikacje Spring bootową + angular 6. Chciałem dodać do niej system powiadomień i zdecydowałem się na webSockety. Jednak mam mały problem przy łączeniu się z nimi z corsami. Zacznijmy od początku.
Konfiguruje corsy:
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfig = new CorsConfiguration().applyPermitDefaultValues();
corsConfig.addAllowedMethod(HttpMethod.OPTIONS);
corsConfig.addAllowedMethod(HttpMethod.PUT);
corsConfig.addAllowedMethod(HttpMethod.DELETE);
corsConfig.addAllowedMethod(HttpMethod.GET);
corsConfig.addAllowedMethod(HttpMethod.POST);
source.registerCorsConfiguration("/**", corsConfig);
return source;
}
Konfiguracja webSocketów:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/app");
config.setApplicationDestinationPrefixes("/");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/websockets")
.setAllowedOrigins("http://localhost:4200")
.withSockJS();
}
}
Połączenie z socketami w js.
connect() {
const socket = new SockJS(this.url + 'websockets');
this.stompClient = Stomp.over(socket);
const _this = this;
this.stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
_this.stompClient.subscribe('/app/notification', function (hello) {
console.log("connector");
});
});
}
I wyskakuje mi takie coś:
Access to XMLHttpRequest at 'http://localhost:8080/websockets/info?t=1548236547075' from origin 'http://localhost:4200' has been blocked by CORS policy: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
stomp.js:174 Whoops! Lost connection to http://localhost:8080/websockets
Link do githuba:
https://github.com/tomaszReda/library