如何为 Spring StandardWebSocketClient 设置代理
How to set proxy for Spring StandardWebSocketClient
我正在尝试使用 Spring StandardWebSocketClient 建立 WebSocket 连接,但由于代理设置而出现错误,因为服务器在代理后面 运行。下面是我正在使用的代码。
StandardWebSocketClient aStandardWebSocketClient=new StandardWebSocketClient();
WebSocketConnectionManager manager = new WebSocketConnectionManager(aStandardWebSocketClient, new WebSockethandler(), aWebSockURL);
设置代理配置成功后可以进行rest call
是否有任何代理配置可用于使 StandardWebSocketClient 连接到 websocket 服务器?
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("proxyHost",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
在筛选 StandardWebSocketClient 实现的源代码后,我认为目前不支持代理设置。但是,我能够在 spring boot:
中使用 jetty
-libs 通过代理建立 wss
连接
pom.xml
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-api</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-common</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
SpringBootJettyWSSclient.java
...
HttpClient httpClient = new HttpClient(new SslContextFactory());
// add proxy
if (this.proxyHost.trim().length() > 0) {
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
List<ProxyConfiguration.Proxy> proxies = proxyConfig.getProxies();
HttpProxy proxy = new HttpProxy(this.proxyHost, Integer.parseInt(this.proxyPort));
proxies.add(proxy);
}
// add proxy auth
if (this.proxyUser.trim().length() > 0) {
String fullProxy = "http://" + this.proxyUser + ":" + this.proxyPassword + "@" + this.proxyHost + ":" + this.proxyPort;
AuthenticationStore authenticationStore = httpClient.getAuthenticationStore();
URI uri = URI.create(fullProxy);
authenticationStore.addAuthentication(new BasicAuthentication(uri, Authentication.ANY_REALM, this.proxyUser, this.proxyPassword));
}
httpClient.start();
WebSocketClient client = new WebSocketClient(httpClient);
client.start();
client.connect(new MyListener(), new URI(this.wssURL));
socket.awaitClose(50, TimeUnit.SECONDS);
...
我正在尝试使用 Spring StandardWebSocketClient 建立 WebSocket 连接,但由于代理设置而出现错误,因为服务器在代理后面 运行。下面是我正在使用的代码。
StandardWebSocketClient aStandardWebSocketClient=new StandardWebSocketClient();
WebSocketConnectionManager manager = new WebSocketConnectionManager(aStandardWebSocketClient, new WebSockethandler(), aWebSockURL);
设置代理配置成功后可以进行rest call
是否有任何代理配置可用于使 StandardWebSocketClient 连接到 websocket 服务器?
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("proxyHost",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
在筛选 StandardWebSocketClient 实现的源代码后,我认为目前不支持代理设置。但是,我能够在 spring boot:
中使用jetty
-libs 通过代理建立 wss
连接
pom.xml
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-api</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-common</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
SpringBootJettyWSSclient.java
...
HttpClient httpClient = new HttpClient(new SslContextFactory());
// add proxy
if (this.proxyHost.trim().length() > 0) {
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
List<ProxyConfiguration.Proxy> proxies = proxyConfig.getProxies();
HttpProxy proxy = new HttpProxy(this.proxyHost, Integer.parseInt(this.proxyPort));
proxies.add(proxy);
}
// add proxy auth
if (this.proxyUser.trim().length() > 0) {
String fullProxy = "http://" + this.proxyUser + ":" + this.proxyPassword + "@" + this.proxyHost + ":" + this.proxyPort;
AuthenticationStore authenticationStore = httpClient.getAuthenticationStore();
URI uri = URI.create(fullProxy);
authenticationStore.addAuthentication(new BasicAuthentication(uri, Authentication.ANY_REALM, this.proxyUser, this.proxyPassword));
}
httpClient.start();
WebSocketClient client = new WebSocketClient(httpClient);
client.start();
client.connect(new MyListener(), new URI(this.wssURL));
socket.awaitClose(50, TimeUnit.SECONDS);
...