在尝试将SockJS客户端连接到spring服务器时遇到错误。 看来SockJS客户端找不到必要的信息-> Network and console display of error
WebSocketConfig.java
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/project");
registry.setApplicationDestinationPrefixes("/app");
}
/*@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.simpDestMatchers("/project/**").permitAll()
.anyMessage().permitAll();
}*/
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/sockjs-endpoint").setAllowedOrigins("http://localhost:3000").withSockJS();
}
}
SockJS客户端
const socketjs2 = new SockJS('http://127.0.0.1:8080/sockjs-endpoint');
export const stomp2 = Stomp.over(socketjs2);
stomp2.connect({}, function (frame) {
console.log("STOMP connected :");
console.log(frame);
stomp2.subscribe('/project/staff', function (requests){
logger(JSON.parse(requests), LOGGING_INFORMATIONS);
}).then(function () {
console.log("sendind to stomp.")
stomp2.send('/app');
});
});
SecurityConfig.java
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
configuration.setAllowCredentials(true);
configuration.setAllowedMethods(Arrays.asList("GET", "PUT", "POST", "DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}