2
即使Spring文檔中沒有描述,websocket連接也會導致連接升級響應(101狀態)。Spring SockJs RequestHandler不會將連接升級到101
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig<S extends ExpiringSession> extends AbstractSessionWebSocketMessageBrokerConfigurer<S>{
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/queue");
config.setApplicationDestinationPrefixes("/mobile-server");
config.setUserDestinationPrefix("/mobile-user");
}
@Override
public void configureStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/ws")
.setHandshakeHandler(new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
.setAllowedOrigins("*")
.withSockJS()
.setSessionCookieNeeded(false)
;
}
}
不過,我得到與由TransportHandlingSockJsService在WebSocketHttpRequestHandler代替生成的「歡迎到SockJS」消息200個狀態這將產生升級AFAIK
@Configuration
public class WebSocketSecurity extends AbstractSecurityWebSocketMessageBrokerConfigurer{
@Override
protected boolean sameOriginDisabled() {
return true;
}
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages
.nullDestMatcher().permitAll()
.simpSubscribeDestMatchers("/user/queue/errors").permitAll()
.simpDestMatchers("/mobile-server/**").hasRole("ENDUSER")
.simpSubscribeDestMatchers("/user/**", "/topic/**").hasRole("ENDUSER")
.anyMessage().denyAll();
}
}
當我更改配置到
@Override
public void configureStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/ws")
.setHandshakeHandler(new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
.setAllowedOrigins("*");
}
令我驚訝的是/ ws調用會導致連接升級101.我很驚訝,因爲文檔和所有示例統一使用withSockJS()和任何websocket連接的開始AFAIK是一個請求升級。
我可以選擇通過連接到/ ws/websocket(也沒有記錄)來強制升級。所以,我不確定什麼是最好的。
有什麼建議嗎?
我建議在此添加文檔。對於新手來說,這非常混亂。 – Marc