Java Authenticator 基于每个连接?

发布于 2024-07-14 07:04:16 字数 934 浏览 6 评论 0原文

我正在构建一个 Eclipse 插件,它与使用基本身份验证的 REST 接口进行通信。 当身份验证失败时,我想弹出插件的设置对话框并重试。 通常我可以使用静态 Authenticator.setDefault() 为所有 HttpURLConnection 设置验证器,但由于我正在编写一个插件,所以我不想覆盖 Eclipse 的默认 Authenticator (org.eclipse.ui.internal.net.auth);

我想在加载之前设置我的自定义 Authenticator,然后将 Eclipse 的默认设置恢复,但我想这会导致多线程的各种竞争问题,所以我很快就失去了这个想法。

谷歌搜索产生的各种结果基本上告诉我这是不可能的:

Java URLConnection API 应该有一个 setAuthenticator(Authenticator) 方法,以便在需要身份验证的多线程上下文中更轻松地使用此类。

来源

如果应用程序包含很少的第三方插件,并且每个插件都使用自己的身份验证器,我们应该做什么? 每次调用“Authenticator.setDefault()”方法都会重写之前定义的Authenticator...

源< /a>

有什么不同的方法可以帮助我克服这个问题吗?

I'm building an Eclipse plugin that talks to a REST interface which uses Basic Authentication. When the authentication fails I would like to popup my plugin's settings dialog and retry. Normally I could use the static Authenticator.setDefault() to setup an authenticator for all HttpURLConnection's for this, but since I am writing a plugin I don't want to overwrite Eclipse's default Authenticator (org.eclipse.ui.internal.net.auth);

I thought of setting my custom Authenticator before loading and putting Eclipse's default back afterwards, but I imagine this will cause all sorts of race issues with multithreading so I quickly lost that notion.

Google searches yield all sorts of results basically telling me it's not possible:

The Java URLConnection API should have a setAuthenticator(Authenticator) method for making it easier to use this class in multi-threaded context where authentication is required.

Source

If applications contains few third party plugins and each plugin use its own Authenticator what we should do? Each invocation of "Authenticator.setDefault()" method rewrite previously defined Authenticator...

Source

Are there any different approaches that might help me overcome this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-07-21 07:04:16

如果 HttpURLConnection 无法实现,我建议使用 Apache 的 httpclient 库。

一个简单的例子:

HttpClient client = new HttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test","test"));
GetMethod getMethod = new GetMethod("http://www.example.com/mylogin");
client.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());

If it is not possible with HttpURLConnection I would suggest using the httpclient library from Apache.

A quick example:

HttpClient client = new HttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test","test"));
GetMethod getMethod = new GetMethod("http://www.example.com/mylogin");
client.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
牵你的手,一向走下去 2024-07-21 07:04:16

另一种方法是自己在连接上执行基本身份验证。

    final byte[] encodedBytes = Base64.encodeData((username + ':' + new String(password)).getBytes("iso-8859-1"));
    final String encoded = new String(encodedBytes, "iso-8859-1");

    connection.setRequestProperty("Authorization", "Basic " + encoded);

这还有一个优点,即在为后续请求提供凭据之前不需要未经身份验证的请求来接收 401。 通过请求抢占式身份验证,可以在 apache http 客户端中利用类似的行为。

Another approach would be to perform the basic authentication yourself on the connection.

    final byte[] encodedBytes = Base64.encodeData((username + ':' + new String(password)).getBytes("iso-8859-1"));
    final String encoded = new String(encodedBytes, "iso-8859-1");

    connection.setRequestProperty("Authorization", "Basic " + encoded);

This would also have the advantage of not requiring an unauthenticated request to receive a 401 before providing the credential on a subsequent request. Similar behavior can be leveraged in the apache http-client by requesting preemptive authentication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文