如何为Spring StandardWebSocketClient设置代理

时间:2018-03-29 19:06:49

标签: spring-boot websocket spring-websocket java-websocket

我正在尝试使用Spring StandardWebSocketClient建立WebSocket连接,但由于服务器在代理后面运行而导致代理设置导致错误。以下是我正在使用的代码。

@Override
public void configure(final AuthorizationServerSecurityConfigurer oauthServer) {
    oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}

可以通过成功设置代理配置来进行休息呼叫。

是否有任何代理配置可用于使StandardWebSocketClient连接到websocket服务器?

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

1 个答案:

答案 0 :(得分:0)

在筛选StandardWebSocketClient实现的源代码之后,我不认为目前支持代理设置。但是我能够通过代理使用wss建立jetty连接 - 在spring boot中使用libs:

<强>的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);
...