5

I am trying to establish a WebSocket connection using Spring StandardWebSocketClient, but getting an error due to proxy settings as the server is running behind the proxy. Below is the code I am using.

StandardWebSocketClient aStandardWebSocketClient=new StandardWebSocketClient();
WebSocketConnectionManager manager = new WebSocketConnectionManager(aStandardWebSocketClient, new WebSockethandler(), aWebSockURL);

Able to make a rest call by setting proxy configuration successfully.

Is there any proxy configuration available for making StandardWebSocketClient connect to websocket server?

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("proxyHost",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
Gangadhar
  • 65
  • 1
  • 8

2 Answers2

0

After Screening the sourcecode of the StandardWebSocketClient implementation I don't think that proxy settings are supported currently. However I was able to establish a wss connection via a proxy using jetty-libs in spring boot:

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);
...
Benvorth
  • 7,416
  • 8
  • 49
  • 70
0

Having had the problem recently, the solution is not easy to find! You need to check the library's classes to see how the headers are passed on. You need to pass a Proxy-Authorization header with basic authentication :

WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.setDefaultMaxTextMessageBufferSize(maxTextMessageSize);
StandardWebSocketClient webSocketClient = new StandardWebSocketClient(container);
if (proxy.isEnabled()) {
    webSocketClient.getUserProperties().put("Proxy-Authorization", proxy.basicAuthorization());
}
WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);

"proxy" being a simple object, mine, containing the configurations of my proxy. If you need :

public String basicAuthorization() {
    String authorizationToEncode = user + ":" + password;
    String authorisationEncoded = Base64.getEncoder().encodeToString(authorizationToEncode.getBytes());
    return "Basic " + authorisationEncoded;
}
MelodyBibi
  • 67
  • 10