我正在尝试实现一个新的微服务,它将是一个WebSocket服务器,假设它运行在http://localhost:8081
上,而WebSocket运行在ws://localhost:8081/ws
上。
客户端将是在不同域上本地运行的Vue.js UI,比如http://dev.domain.com:8080
。
当我点击UI按钮,将触发WebSocket连接和数据传输,我得到以下在Chrome:Common.umd.js:8 WebSocket connection to 'ws://localhost:8081/ws' failed:
在Firefox中:
GET ws://localhost:8081/ws [HTTP/1.1 403 Forbidden 412ms]
Firefox can’t establish a connection to the server at ws://localhost:8081/ws. Common.umd.js:formatted:3191:34
在Firefox中,它给出了一个403
错误,但在Chrome中,在网络选项卡下,它只是说对ws://localhost:8081/ws
的请求有Finished
,没有状态代码。我真的不知道这是怎么回事,但403
肯定来自于403
服务器,对吗?
WebSocket服务器代码通常看起来像这样(我省略了细节,因为在Websocket服务器访问日志中没有提到403
,这意味着代码甚至没有达到):
@ServerWebSocket("/ws")
public class Server {
/** Class logger. */
private static final Logger log = LoggerFactory.getLogger(Server.class);
/** Micronaut application context. */
@Inject ApplicationContext ctx;
/** Service configuration. */
@Inject Configuration configuration;
/** WebSocket broadcaster. */
@Inject WebSocketBroadcaster broadcaster;
/** The RabbitMQ message producer. */
@Inject ApiProducer producer;
/**
* Handler for connection open event.
*
* @param session - the created WS session.
* @return Publisher for the startup confirmation message.
*/
@SuppressWarnings("static-method")
@OnOpen
public Publisher<String> onOpen(WebSocketSession session) {
...
}
/**
* Handler for message received event.
*
* @param message - the command payload
* @param session - current WS session
* @return Publisher for the response message.
*/
@SuppressWarnings("unchecked")
@OnMessage
public Publisher<String> onMessage(String message, WebSocketSession session) {
...
}
public Mono<String> onNotification(String notification, UUID correlationId) {
...
}
/**
* Handler for connection close event.
*
* @param session - the WS session to be closed.
*/
@SuppressWarnings("static-method")
@OnClose
public void onClose(WebSocketSession session) {
...
}
}
在Vue.js方面,我们使用vuex
createStore
方法与vue-native-websocket-vue3
包中的VueNativeSock
模块结合使用。
同样,在application.yml
文件中,我尝试启用CORS请求的处理,如下所示:
micronaut:
application:
name: application-name
server:
cors:
enabled: true
port: 8081
然而,这仍然行不通。
一些附加上下文:当我使用http://localhost:8080
作为UI域并保持其他一切相同时,它可以连接并发送数据,没有问题。只有当UI具有不同的域时才会出现问题,这在生产中也是如此。
任何帮助都是感激不尽的。
1条答案
按热度按时间6jygbczu1#
所以我在这里找到了答案:https://github.com/micronaut-projects/micronaut-core/issues/8560在sdelamo 1月11日的第一条评论中。
我需要用途:
这与我读到的文档相矛盾,文档中说configurations属性用于限制cors访问特定的域不允许它。