Java websocket client with spnego

Viewed 423

I need to connect to a websocket service which uses spnego authentication from a stand alone java process.

Can anyone provide a java example or point me at a library that has this functionality please?

I can query a rest api on the same server using SpnegoHttpURLConnection - so the spnego kerberos part works.
I can also connect to the websocket if I disable the authentication on the server.

However SpengoHttpURLConnection doesn't have any easy way to upgrade the connection to a websocket, and I can't find a websocket client library that lets me configure spnego authentication.

1 Answers

You can generate the SPNEGO token using Kerb4J and add it to headers using ClientEndpointConfig.Configurator.beforeRequest(Map> headers) method.

If your websocket is running on wss://ws.server.com and you authenticate using svc_consumer account and /opt/myapp/consumer.keytab keytab the code would be like this:

SpnegoClient spnegoClient = SpnegoClient.loginWithKeyTab("svc_consumer", "/opt/myapp/consumer.keytab");

@Override
public void beforeRequest(Map<String,List<String>> headers) {
    SpnegoContext context = spnegoClient.createContext("https://ws.server.com");
    headers.put("Authorization", Collections.singletonList(context.createTokenAsAuthroizationHeader()));
}

Disclaimer: I'm the author of Kerb4J

Related