我正在尝试对使用 REST 在我的 jboss 上运行的登录模块执行基本身份验证。我已经找到了一个 StackOverflow 主题,它解释了如何使用凭据进行身份验证。
RESTEasy client framework authentication credentials
这不起作用。分析与 Wireshark 建立的连接我无法看到带有 Authorization: Basic 的 HTTP 包。经过更多研究,我发现了这篇文章,http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html/RESTEasy_Client_Framework.html它描述了如何将基本身份验证附加到 ApacheHttpClient4Executor
从安逸。
// Configure HttpClient to authenticate preemptively
// by prepopulating the authentication data cache.
// 1. Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// 2. Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put("com.bluemonkeydiamond.sippycups", basicAuth);
// 3. Add AuthCache to the execution context
BasicHttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
// 4. Create client executor and proxy
httpClient = new DefaultHttpClient();
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(httpClient, localContext);
client = ProxyFactory.create(BookStoreService.class, url, executor);
但这也不起作用。没有描述如何将基本身份验证的用户名和密码附加到构造中。为什么该信息与
httpcomponent
中的任何类都没有关联? ?
最佳答案
可以使用org.jboss.resteasy.client.jaxrs.BasicAuthentication
它与 resteasy-client 3.x 一起打包,专门用于基本身份验证。
Client client = ClientBuilder.newClient();
ResteasyWebTarget resteasyWebTarget = (ResteasyWebTarget)client.target("http://mywebservice/rest/api");
resteasyWebTarget.register(new BasicAuthentication("username", "passwd"));
关于basic-authentication - 使用 Resteasy 客户端进行基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095289/