4

I'm a long time reader and first time user so please go easy on me.

I'm attempting to use preemptive auth with javax.ws.rs.client.Client. I've got this working with HTTPClient, but I can't figure out how to accomplish the same with a JAX authenticator.

    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    Credentials creds = new UsernamePasswordCredentials("user", "pass");
    client.getState().setCredentials(AuthScope.ANY, creds);

I do have basic auth working with JAX. Here's my client:

    public HttpsConnection() {
        // configure ssl
        final SslConfigurator sslConfig = SslConfigurator.newInstance().keyStoreFile(keyStore).keyPassword("pass");

        final HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        final SSLContext sslContext = sslConfig.createSSLContext();

        // configure client
        final Client client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(hostnameVerifier)
            .register(new Authenticator(username, password)).register(JacksonFeature.class)
            .register(new JsonObjectMapper()).build();

        WebTarget target = client.target(base_url);
    }

And here's my auth filter:

public class Authenticator implements ClientRequestFilter {

    private final String user;
    private final String password;

    public Authenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    public void filter(ClientRequestContext requestContext) throws IOException {
        MultivaluedMap<String, Object> headers = requestContext.getHeaders();
        final String basicAuthentication = getBasicAuthentication();
        headers.add("Authorization", basicAuthentication);
    }

    private String getBasicAuthentication() {
        String token = this.user + ":" + this.password;
        try {
            return "Basic " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException("Cannot encode with UTF-8", ex);
        }
    }
}

Can anyone give me an example using UsernamePasswordCredentials with preemptive auth like I'm doing with HTTPClient above? I must be Googling for all of the wrong things because I just can't find an example.

And if my post totally sucks then please let me know before it gets closed by an OP =)

catphysh
  • 41
  • 2
  • Hello, your question doesn't suck at all!! In fact I have exactly the same problem. I have my code working like you and want to migrate it to JAX-RS 2.0 but I'm getting 401 Unauthorised error. Did you get to solve this? Thanks. – icordoba Jul 10 '15 at 11:12
  • I ended up going a different route since I was working in Spring. Been a long time and I no longer have access to source, but I do recall that this link was helpful... [link](http://forum.spring.io/forum/spring-projects/web/114029-preemptive-basic-authentication-with-resttemplate) – catphysh Jul 11 '15 at 16:14
  • Hi, I staid with JAX-RS (Jersey) using the following HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(userName, userPassword); and then ClientBuilder.newClient().register(feature). That did the trick. More on http://howtodoinjava.com/jersey/jersey-restful-client-api-authentication-example/ – Thomas Jul 20 '17 at 14:31

0 Answers0