Spring security Spring安全性和Stomp未看到标题
我有一个spring安全应用程序,我正在尝试使用Stomp实现WebSocket 该应用程序主要基于REST,使用令牌进行安全保护。所有传入的请求都必须在头中添加一个安全令牌 问题是,当使用基本html设置一个简单的Stomp客户端时,spring似乎看不到任何标题 如果我禁用安全性,客户机工作正常,在这种情况下,不会传入任何头Spring security Spring安全性和Stomp未看到标题,spring-security,stomp,Spring Security,Stomp,我有一个spring安全应用程序,我正在尝试使用Stomp实现WebSocket 该应用程序主要基于REST,使用令牌进行安全保护。所有传入的请求都必须在头中添加一个安全令牌 问题是,当使用基本html设置一个简单的Stomp客户端时,spring似乎看不到任何标题 如果我禁用安全性,客户机工作正常,在这种情况下,不会传入任何头 var socket = new SockJS('http://localhost:8080/project/ws/wsendpoint'); var headers
var socket = new SockJS('http://localhost:8080/project/ws/wsendpoint');
var headers = {'Auth': 'some_auth_token'}
writeConsole("Created socket");
stompClient = Stomp.over(socket);
stompClient.connect(headers, function(frame) {
writeConsole("Connected to via WebSocket");
stompClient.subscribe('/topic/push', function(message)
{ writeConsole(message.body);}, headers );
});
window.onbeforeunload = disconnectClient;
以下是相关的弹簧配置
protected void configure(final HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.headers().frameOptions().sameOrigin()
.and()
.authorizeRequests()
.anyRequest().authenticated() authenticated.
.and()
.anonymous().disable()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());
http.addFilterBefore(authenticationTokenFilter(), BasicAuthenticationFilter.class);
}
authenticationTokenFilter类中的doFilter应该看到客户端中设置的标题字段“Auth”,但是没有任何内容。Stomp无法在初始身份验证阶段发送任何自定义标题。解决方法是将身份验证令牌作为查询参数发送(不建议非封闭系统使用)。您可以用自己的ID替换sessionId,而不是发送头
var sessionId = utils.random_string(36);
var socket = new SockJS('/socket', [], {
sessionId: () => {
return sessionId
}
});
stompClient = Stomp.over(socket);
stompClient.connect(headers, function(frame) {
writeConsole("Connected to via WebSocket");
stompClient.subscribe('/topic/push', function(message)
{ writeConsole(message.body);}, headers );
});
使用wireshark,似乎没有发送标头字段,这表明问题出在客户端代码中。我看过Stomp文档、代码和示例,我看不出我所做的有任何问题。当在SockJs客户端上添加头时,它会将它们作为属性添加到头“native headers”下。IE:'nativeHeaders={token=[testtoken],accept version=[1.1,1.0],heartbeat=[1000010000]}。请注意,“token”是我在客户端添加的自定义标头。是否可以将其作为查询参数而不是标头传入?这是可以做到的。或者如果你解决了这个问题,我想知道你最后做了什么。嗨,这就是我们最后做的。