我遇到javax.websocket问题(使用Eclipse IDE和Jetty 9服务器)。 我写了ClientEnpdoint(带有所有注释)。此代码适用于" ws://"但我在尝试使用时遇到问题" wss://"。
我尝试使用SSLContext,但不知道如何将SSLContextFactory添加到会话或套接字容器中。
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.security.KeyStore;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import javax.websocket.*;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.WebSocketContainer;
@ClientEndpoint
public class ScriptSocketJavax extends Thread{
final static Logger logger = LogManager.getLogger(ScriptSocketJavax.class);
public int checkWork;
private String type;
private String opertr;
private String client;
private Date date = new Date();
private SimpleDateFormat formatForDateNow = new SimpleDateFormat("MM/dd/yyyy");
private Session session;
private URI uri;
WebSocketContainer container;
ClientEndpointConfig endpointConfig;
public ScriptSocketJavax(String opertr, String client, String type) throws Exception{
this.checkWork = 0;
this.uri = URI.create("wss://*****"); //ws://****
this.type = type;
this.client = client;
this.opertr = opertr;
this.container = ContainerProvider.getWebSocketContainer();
this.container.setDefaultMaxTextMessageBufferSize(1024*1024);
this.container.setDefaultMaxBinaryMessageBufferSize(1024*1024);
try {
String STORETYPE = "JKS";
String KEYSTORE = "C:\\****";
String STOREPASSWORD = "123456";
String KEYPASSWORD = "123456";
KeyStore ks = KeyStore.getInstance(STORETYPE);
ks.load(new FileInputStream(KEYSTORE), STOREPASSWORD.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, KEYPASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
sslContext.getSocketFactory();
SSLSocketFactory factory = sslContext.getSocketFactory();
this.session = this.container.connectToServer(this, this.uri); //obj, not class
或如何让所有连接受信任?
堆栈跟踪:
java.io.IOException: Connect failure
at org.eclipse.jetty.websocket.jsr356.ClientContainer.connect(ClientContainer.java:231)
at org.eclipse.jetty.websocket.jsr356.ClientContainer.connectToServer(ClientContainer.java:261)
at com.stepanov.utils.ScriptSocketJavax.<init>(ScriptSocketJavax.java:80)
...
at java.lang.Thread.run(Unknown Source)
Caused by: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
...
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:513)
... 3 more
Caused by: javax.net.ssl.SSLHandshakeException: General SSLEngine problem
...
... 15 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 23 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 29 more
答案 0 :(得分:0)
首先,您的JAVA应该信任URI的证书。要使它受JAVA信任,您需要将证书添加到Java密钥库。