首页 > 解决方案 > WebSocket 握手期间出错:“Sec-WebSocket-Accept”标头值不正确

问题描述

我有一个 spring-boot websocket 连接,它位于 spring-security-kerberos 后面以实现 SSO。这可以按预期工作,但是如果我重新启动服务器,我会看到客户端无法重新连接并出现错误Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

我正在使用@stomp/stompjs 4.0.8 并设置stompClient.reconnect_delay = 5000

有没有办法解决这个问题?我担心在负载均衡器后面运行它会导致这个错误一直发生。

这是基于messaging-stomp-websocket示例+ spring-security websocket-authentication

标签: spring-bootspring-securitystompjs

解决方案


似乎 spring-security-webRequestCacheAwareFilter提取了一个缓存的请求,导致实际的Sec-WebSocket-Key标头值被无效的值替换。

事件的顺序是,每次客户端尝试重新连接时,客户端发出两个 websocket 请求,第一个被WWW-Authenticate: Negotiate header 拒绝,第二个包含Authorization header 有不同的Sec-WebSocket-Key价值。

我能够通过完全禁用缓存来解决这个问题,例如在WebSecurityConfigurerAdapter

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http.requestCache().requestCache(new NullRequestCache())
}

推荐阅读