java RESTEasy 客户端框架身份验证凭据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1885844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:23:03  来源:igfitidea点击:

RESTEasy client framework authentication credentials

javahttpjax-rsresteasy

提问by jnorris

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

RESTEasy(一个 JAX-RS 实现)有一个很好的客户端框架,例如:

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

How do you provide HTTP authentication credentials to this client?

您如何向此客户端提供 HTTP 身份验证凭据?

采纳答案by jnorris

Credentials can be provided by using ClientExecutor.

可以使用 ClientExecutor 提供凭据。

   Credentials credentials = new UsernamePasswordCredentials(userId, password);
   HttpClient httpClient = new HttpClient();
   httpClient.getState().setCredentials(AuthScope.ANY, credentials);
   httpClient.getParams().setAuthenticationPreemptive(true);

   ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);

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

回答by Z. Cass

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

jnorris 的回答使用了一些不推荐使用的类。这是使用未弃用类的更新方法。

    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);

回答by Marco

Maybe something like this (with RestEasy 2.3.1) :

也许是这样的(使用 RestEasy 2.3.1):

//(...imports...)
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;

//(...code...)
private static final String ENDPOINT = "the_rest_endpoint_here";

String username = "<user_name>";
String password = "<password>";

DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(username, password));
ApacheHttpClient4Executor executer = new ApacheHttpClient4Executor(client);

ClientRequest req = new ClientRequest(ENDPOINT, executer);

ClientResponse<String> res = req.get(String.class);
System.out.println(res.getEntity());
res.close();

...or with RestEasy 3.0.18

...或使用 RestEasy 3.0.18

//(...imports...)
import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.BasicAuthentication;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

//(...code...)
private static final String ENDPOINT = "the_rest_endpoint_here";

String usertoken = "<user_token>";
String password = "<password>";

//with proxy(comment this if not using)
ResteasyClient client = new ResteasyClientBuilder()
            .defaultProxy("my_proxy", 8080, "http")
            .build();

ResteasyWebTarget target = client.target(ENDPOINT);
target.register(new BasicAuthentication(usertoken, password));

Response response = target.request().get();
System.out.println(response.readEntity(String.class));
response.close();