在AngularJS中使用Spring Websocket和SockJS时获得403状态

时间:2018-01-25 04:16:28

标签: angularjs spring websocket spring-websocket sockjs

我收到此错误,我的网络套接字无法运行: 403

这是我对websocket的配置:

@Configuration
@EnableWebSocketMessageBroker
public class SpringWebSocketConfig extends 
AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setUserDestinationPrefix("/ws-secured/user/");
    config.setApplicationDestinationPrefixes("/ws");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws-secured/init-api").withSockJS();
    registry.setErrorHandler(new ApplicationStompSubProtocolErrorHandler());
}

}

和websocket security:

@Configuration
public class SpringWebSocketSecurityConfig extends 
AbstractSecurityWebSocketMessageBrokerConfigurer {
 @Override
 protected void configureInbound(MessageSecurityMetadataSourceRegistry 
messages) {
 messages.simpTypeMatchers(
        SimpMessageType.CONNECT,
        SimpMessageType.MESSAGE,
        SimpMessageType.SUBSCRIBE).authenticated()
 .simpTypeMatchers(
        SimpMessageType.UNSUBSCRIBE,
        SimpMessageType.DISCONNECT).permitAll()
 .anyMessage().denyAll();

}

 @Override
 protected boolean sameOriginDisabled() {
  return true;
 }

}

有关更多信息,请参阅Spring Sec中的csrf configure:

http.headers().frameOptions().sameOrigin().and().authorizeRequests();
http
.csrf()        
    .ignoringAntMatchers("/ws-secured/**")
    .and()
.headers()
    .frameOptions().sameOrigin();

我还将我的csrf标记传递给标题:

$stomp.connect('/ws-secured/init-api', {'X-CSRF-TOKEN' : csrf_token}, function(data) {

我在互联网上搜索但找不到答案。我正在使用Tomcat 7.你有什么想法吗?

0 个答案:

没有答案