basic-authentication - 使用 Resteasy 客户端进行基本身份验证

标签 basic-authentication resteasy

我正在尝试对使用 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/

相关文章:

java - ClassNotFoundException 与 ResteasyClientBuilder (JBOSS)

带有授权 header 的 JavaScript 重定向 URL

http - 如何获得浏览器身份验证弹出窗口?

objective-c - 带凭据的 AFNetworking 上传任务(HTTP 基本身份验证)

java - Resteasy ExceptionMapper 不返回结果

java - 无法从 START_OBJECT token 中反序列化 java.util.ArrayList 的实例

apache:重写前的基本身份验证

c# - Web Api 不会使用 jQuery Ajax 和 Basic Auth 下载文件

javax.ws.rs.ProcessingException : RESTEASY004655: Unable to invoke request

java - 在 Quarkus 应用程序中注入(inject) ServletContext