是否可以在 Spring Boot 中使用码头服务器在 Stomp 客户端/服务器中配置相互身份验证 wss?
我试过这个没有成功。
在客户端代码中,我尝试使用客户端中的用户属性设置 SSLContext。另一个在线示例仅使用 tomcat 属性。
@Service
@Slf4j
public class StompClient {
@Value("${web-socket.server.endpoint}")
private String URL;
private WebSocketStompClient stompClient;
private StompSession stompSession;
private MutualAuthConfiguration mutualAuthConfiguration;
@Autowired
@Qualifier("MyStompSessionHandler")
private StompSessionHandler sessionHandler;
@Autowired
public StompClient(MutualAuthConfiguration mutualAuthConfiguration) throws GeneralSecurityException, IOException {
this.mutualAuthConfiguration = mutualAuthConfiguration;
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(mutualAuthConfiguration.getTrustStore().getURL(), mutualAuthConfiguration.getTrustStorePassword().toCharArray())
.loadKeyMaterial(mutualAuthConfiguration.getKeyStore().getURL(), mutualAuthConfiguration.getKeyStorePassword().toCharArray(), mutualAuthConfiguration.getKeyPassword().toCharArray())
.build();
StandardWebSocketClient wsClient = new StandardWebSocketClient();
//FIXME is OK? i don't find correct properties for jetty.
wsClient.getUserProperties().put("org.eclipse.jetty.server.SslConnectionFactory", sslContext);
List<Transport> transports = new ArrayList<>(2);
transports.add(new WebSocketTransport(wsClient));
transports.add(new RestTemplateXhrTransport());
WebSocketClient client = new SockJsClient(transports);
stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
}
}
如何为 wss/ssl 配置服务器部分?我没有找到任何配置信息
@Configuration
@EnableWebSocketMessageBroker
@Slf4j
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-test")
.withSockJS()
.setHeartbeatTime(1000).setWebSocketEnabled(true);
}
最佳答案
经过几次不成功的测试后,我切换到了这个库
https://github.com/TooTallNate/Java-WebSocket
这让我可以做我需要的一切。
唯一没有内置在 spring 中的问题。
关于Spring STOMP 配置相互认证 wss/ssl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69175581/