目前我有一些使用resteasy调用的uris,看起来像这样:
http://host.com/api/project1/getsomestuff
和
http://host.com/api/project2/getsomestuff
两者都使用摘要身份验证,但需要不同的用户名和密码进行身份验证。目前在代码中,我必须通过为每个项目创建不同的客户端实例来处理这个问题,如下所示:
DefaultHttpClient project1Client = new DefaultHttpClient();
Credentials project1Credentials = new UsernamePasswordCredentials("user1", "password1");
project1Client.getCredentialsProvider().setCredentials(AuthScope.ANY, project1Credentials);
ClientExecutor executor1 = new ApacheHttpClient4Executor(project1Client);
MyService project1Proxy = ProxyFactory.create(MyService.class, executor1);
project1Proxy.getSomeStuff("project1");
DefaultHttpClient project2Client = new DefaultHttpClient();
Credentials project2Credentials = new UsernamePasswordCredentials("user2", "password2");
project2Client.getCredentialsProvider().setCredentials(AuthScope.ANY, project1Credentials);
ClientExecutor executor2 = new ApacheHttpClient4Executor(project1Client);
MyService project2Proxy = ProxyFactory.create(MyService.class, executor2);
project2Proxy.getSomeStuff("project2");
我查看了 AuthScope 和 CredentialsProvider ,但我看不出这怎么可能。有谁知道使用同一客户端使用两组凭据的任何方法,还是我被困在使用多个客户端?
另外,请注意,我不控制此 rest 调用的服务器端,并且两个调用使用相同的领域,因此我无法为每个领域设置不同的凭据。