我可以连接客户端服务器(localhost:8082)和 websocket(localhost:8080)吗?
can i connect client server(localhost:8082) and websocket(localhost:8080)?
我的问题是我需要连接不同的端口或服务器!
websocket 服务器(localhost:8080) 客户端服务器(localhost:random)
(失败:WebSocket 握手期间出错:意外响应代码:403)
为什么??我累了...
我已经尝试过相同的端口,我可以成功!
我可以连接 client(localhost:8080) 和 websocekt(localhost:8080)。
我想使用 websocket 使用(服务器端:java sts,tomcat 8.0)。
问题
1. websocket 只能连接相同的服务器和端口???!!!!(不!拜托!)
2. 如果我可以...有什么问题:(?你有什么例子吗?
听起来这与 CORS https://en.wikipedia.org/wiki/Cross-origin_resource_sharing 有关。
我假设您正在使用 spring websocket,因为您没有提到。
您需要在您的 websocket 配置中禁用同源策略 class 如下例,
@配置
public class WebSocketSecurityConfig 扩展了 AbstractSecurityWebSocketMessageBrokerConfigurer {
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
}
@Override
protected boolean sameOriginDisabled() {
//disable CSRF for websockets
return true;
}
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}
我的问题是我需要连接不同的端口或服务器! websocket 服务器(localhost:8080) 客户端服务器(localhost:random) (失败:WebSocket 握手期间出错:意外响应代码:403) 为什么??我累了...
我已经尝试过相同的端口,我可以成功! 我可以连接 client(localhost:8080) 和 websocekt(localhost:8080)。
我想使用 websocket 使用(服务器端:java sts,tomcat 8.0)。
问题 1. websocket 只能连接相同的服务器和端口???!!!!(不!拜托!) 2. 如果我可以...有什么问题:(?你有什么例子吗?
听起来这与 CORS https://en.wikipedia.org/wiki/Cross-origin_resource_sharing 有关。 我假设您正在使用 spring websocket,因为您没有提到。
您需要在您的 websocket 配置中禁用同源策略 class 如下例,
@配置 public class WebSocketSecurityConfig 扩展了 AbstractSecurityWebSocketMessageBrokerConfigurer {
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
}
@Override
protected boolean sameOriginDisabled() {
//disable CSRF for websockets
return true;
}
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}