Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - RESTEasy client framework authentication credentials

RESTEasy (a JAX-RS implementation) has a nice client framework, eg:

ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);

How do you provide HTTP authentication credentials to this client?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jnorris's answer uses some deprecated classes. Here is an updated way that uses non-deprecated classes.

    import org.apache.http.HttpStatus;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.impl.client.DefaultHttpClient;
    ...
    DefaultHttpClient httpClient = new DefaultHttpClient();

    Credentials credentials = new UsernamePasswordCredentials(userName,
            password);
    httpClient.getCredentialsProvider().setCredentials(
            org.apache.http.auth.AuthScope.ANY, credentials);

    ClientExecutor clientExecutor = new ApacheHttpClient4Executor(
            httpClient);
    proxy = ProxyFactory
            .create(UserAccessProxy.class, host, clientExecutor);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...