spring-rsocket 是否支持 return @ConnectMapping 注释方法中的 RejectedSetupException?

Does spring-rsocket support return a RejectedSetupException in @ConnectMapping annotated method?

我按照 spring-rsocket-demo 项目来完成我的代码。我在我的服务器端代码中添加了一些身份验证逻辑,如下所示。你可以看到我在验证逻辑 'CLIENT.add(requester)' 之后抛出了一个异常。我在名为 'testRequestGetsResponse' 的 spring-rsocket-demo 项目中执行测试方法,我发现我无法接收到异常。如何处理 @ConnectMapping 注释方法中的设置有效负载和 return RejectedSetupException(如果需要)。


        requester.rsocket()
                .onClose()
                .doFirst(() -> {
                    // Add all new clients to a client list
                    log.info("Client: {} CONNECTED.", client);
                    //throw Exception during process setup payload
                    //my authentication code.
                    try {
//                        CLIENTS.add(requester);
                        throw new RuntimeException();
                    } catch (Exception exception) {
                        throw exception;
                    }
                })
                .doOnError(error -> {
                    // Warn when channels are closed by clients
                    log.warn("Channel to client {} CLOSED", client);
                })
                .doFinally(consumer -> {
                    // Remove disconnected clients from the client list
                    CLIENTS.remove(requester);
                    log.info("Client {} DISCONNECTED", client);
                })
                .subscribe();

您应该按如下方式定义 @ConnectionMapping 方法:

@ConnectionMapping 
Mono<Void> handleSetup(@Payload String payload) {
   // Add all new clients to a client list
   log.info("Client: {} CONNECTED.", client);
   boolean isAuthenticated = ... //my authentication code.
   if (!isAuthenticated) {
     //throw Exception during process setup payload
     return Mono.error(new RejectedSetupException("connection is not authenticated"));
   }

   // add client if it is authenticated
   CLIENTS.add(requester);

   requester.rsocket()
            .onClose()
            .doFinally(consumer -> {
               // Remove disconnected clients from the client list
               CLIENTS.remove(requester);
               log.info("Client {} DISCONNECTED", client);
            })
            .subscribe();

   return Mono.empty();
}

正如您从上面的代码中看到的,如果连接没有通过身份验证,它 returns 一个 Mono.error 并带有适当的错误