RESTEasy가 (A JAX-RS 구현)는 좋은 클라이언트 프레임 워크를 가지고 예 :RESTEasy가 클라이언트 프레임 워크 인증 자격 증명
ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);
방법이 클라이언트에 HTTP 인증 자격 증명을 제공합니까?
RESTEasy가 (A JAX-RS 구현)는 좋은 클라이언트 프레임 워크를 가지고 예 :RESTEasy가 클라이언트 프레임 워크 인증 자격 증명
ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);
방법이 클라이언트에 HTTP 인증 자격 증명을 제공합니까?
자격 증명은 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);
jnorris의 대답은 일부 비추천 클래스를 사용합니다. 다음은 비 deprecated 클래스를 사용하는 업데이트 된 방법입니다.
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);
아마 (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();
... 또는
//(...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();
RESTEasy가 3.0.18로