简体   繁体   English

使用StompClient的SSL Websocket — SSL证书异常

[英]SSL Websocket using StompClient — SSL Certificate exception

I have simple Java StompClient connecting to Java websocket events. 我有简单的Java StompClient连接到Java websocket事件。 It works when server is configured as ws. 服务器配置为ws时,它可以工作。 Not able to connect when server is configured as wss. 将服务器配置为wss时无法连接。 Code snippted below... 代码片段如下...

KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(this.getClass().getResourceAsStream("/truststore.jks"), "<hidden_pwd_for_thispost>".toCharArray());
KeyStore keystore = KeyStore.getInstance("JKS");;
keystore.load(this.getClass().getResourceAsStream("/keystore.jks"), "<hidden_pwd_for_thispost>".toCharArray());

SSLContext sslContext = new 
SSLContextBuilder().loadTrustMaterial(truststore, acceptingTrustStrategy);
            .loadKeyMaterial(keystore, "<hidden_pwd_forthisPost>".toCharArray()).build();
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
StandardWebSocketClient client = new StandardWebSocketClient();
client.getUserProperties().clear();
client.getUserProperties().put("org.apache.tomcat.websocket.SSL_CONTEXT", sslContext);
WebSocketStompClient stompClient = new WebSocketStompClient(client);   
ListenableFuture<StompSession> sessionFuture = stompClient.connect(url, handler);
session = sessionFuture.get();

Exception 例外

Caused by: java.security.cert.CertificateException: No name matching <myhost> found
at sun.security.util.HostnameChecker.matchDNS(Unknown Source)
at sun.security.util.HostnameChecker.match(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
at sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(Unknown Source)
at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)

Please note I have build keyStore and selfSigned trustStore locally. 请注意,我在本地构建了keyStore和selfSigned trustStore。 Anf both keyStore and trustStore has CN as my hostname. keyStore和trustStore都将CN作为我的主机名。 Verified above by running keytool -list 上面通过运行keytool -list进行了验证

Can some please suggest. 可以请一些建议。 Your help much appreciated. 非常感谢您的帮助。

Apologies if this question is already answered, i have searched for while with no result. 抱歉,如果这个问题已经回答,我已经搜索了一段时间,但没有结果。 Hence posting. 因此发布。

Thanks, 谢谢,

Does the Subject Alternative Name in your certificate match the Server Host name? 证书中的使用者备用名称是否与服务器主机名匹配?

X509v3 extensions:
    X509v3 Subject Alternative Name:
        DNS:<**HOST name which matches the server host name**>

You can check this by using below steps: 您可以通过以下步骤进行检查:

•   openssl s_client -connect <serverhostname>:<port on which you connect>
•   Copy the string from -----BEGIN CERTIFICATE-----  till     -----END CERTIFICATE-----
•   Paste the string into a .pem file, for example:- “test.pem”
•   Run command:   openssl x509 -in test.pem -noout -text

If SAN doesn't have the host name, then DN comes into picture: Refer this:- CertificateException: No name matching ssl.someUrl.de found 如果SAN没有主机名,则会出现DN:请参阅此: -CertificateException:找不到与ssl.someUrl.de匹配的名称


UPDATE: 更新:

If you are trying to achieve mutual authentication, that means both the server and the client should have their own certificate. 如果您试图实现相互身份验证,则意味着服务器和客户端都应具有自己的证书。 Based on the code _stompClient.connect(url, handler); 基于代码_stompClient.connect(url,handler); it seems to me that the Websocket server called by StompClient is acting as the server here, and your calling code is the client. 在我看来,由StompClient调用的Websocket服务器在这里充当服务器,而您的调用代码是客户端。 Based on this understanding, you must configure your certificates correctly. 基于此了解,您必须正确配置证书。 I think you need to provide more details to the question to clarify how you have set things up. 我认为您需要为该问题提供更多详细信息,以阐明如何进行设置。 SSL is a complex topic, even a slight mistake in configuration can result in an error.(evident from the fact that WS is working but not WSS) SSL是一个复杂的主题,即使配置中的轻微错误也可能会导致错误。(从WS工作但没有WSS的事实可以看出)


UPDATE: 更新:

Based on your updated comment, it means that client certificate is not in picture, so no mutual authentication, but only server certificate will be used by the client, and it will check if it trusts the server certificate which means the CA which signs the server certificate should be present in the client's trust store. 根据您更新的注释,这意味着客户端证书不在图中,因此没有相互身份验证,而是客户端仅使用服务器证书,并且它将检查是否信任服务器证书,这意味着对服务器进行签名的CA证书应存在于客户的信任库中。 If your server is accessible as a web-server you could try opening it via the browser and checking if it shows a valid certificate if you use https to access any resource/UI. 如果您的服务器可以作为Web服务器访问,则可以尝试通过浏览器将其打开,并检查是否使用https访问任何资源/ UI时是否显示有效的证书。 That should at-least help in figuring out whether the certificate is properly configured or not. 这至少应有助于确定证书是否正确配置。

问题已解决.....请检查评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM