JCIFS 可以与 Jersey 一起使用吗? [英] Can JCIFS be used with Jersey?

查看:64
本文介绍了JCIFS 可以与 Jersey 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法向使用 Jersey 和 Apache HttpClient 的现有应用程序添加 NTLM 身份验证.我只能使用 JCIFS 进行身份验证,来自 HttpClient 的默认 NTLM 身份验证不起作用(我得到 401).

I have trouble adding NTLM authentication to my existing application that uses Jersey and Apache HttpClient. I was only able to authenticate using JCIFS, the default NTLM authentication from HttpClient does not work (I get 401).

Apache HttpClient 页面的示例展示了如何使用 CloseableHttpClient:https://hc.apache.org/httpcomponents-client-4.5.x/ntlm.html

The example from Apache HttpClient page shows how to use CloseableHttpClient: https://hc.apache.org/httpcomponents-client-4.5.x/ntlm.html

Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
    .register(AuthSchemes.NTLM, new JCIFSNTLMSchemeFactory())
    .register(AuthSchemes.BASIC, new BasicSchemeFactory())
    .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
    .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
    .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
    .build();
CloseableHttpClient httpClient = HttpClients.custom()
    .setDefaultAuthSchemeRegistry(authSchemeRegistry)
    .build();

但是对于 CloseableHttpClient 我不能使用像 target 这样的方法:

But with CloseableHttpClient I cannot use methods like target:

WebTarget target = client.target(this.my Address).path(elementPath)
            .resolveTemplate(P_ID, myId);

只有execute.

我不确定是否应该重写整个应用程序并仅使用基本的 HttpClient 调用,例如:

I'm not sure if I should rewrite my whole application and use only basic HttpClient calls like:

HttpGet httpGet = new HttpGet(repositoryAddress + "/" + "element/70032_1498404600000(,,arm)");
CloseableHttpResponse response = httpClient.execute(httpGet);

或者有其他方法可以在javax.ws.rs.client.Client中设置AuthSchemes,可以在Jersey中使用吗?

or there is some other way to set AuthSchemes in javax.ws.rs.client.Client, which can be used in Jersey?

推荐答案

我遇到了类似的问题,我的方法如下:

I faced similar issues and my approach was below :

1) 如果您使用 ApacheConnectorProvider 作为连接器,您可以覆盖 ApacheConnector 代码(在此处找到 https://github.com/jersey/jersey/tree/master/connectors/apache-connector/src/main/java/org/glassfish/球衣/阿帕奇/连接器).就我而言,我必须创建自定义 ConnectorProvider 和 Connector.

1) If you are using ApacheConnectorProvider as Connector you can Override ApacheConnector code (found here https://github.com/jersey/jersey/tree/master/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector). In my case I had to create custom ConnectorProvider and Connector.

2) 创建自定义属性或使用 HttpClientContext.AUTHSCHEME_REGISTRY 并将其放入 ClientConfig (这就是我们在 Jersey Client 中为客户端设置属性的方式).

2) Create a custom property or use HttpClientContext.AUTHSCHEME_REGISTRY and put it in the ClientConfig ( This is how we set properties for client in Jersey Client).

3) 当您调用 builder.get( 或 post 或任何其他方法) 时,会调用自定义连接器.在自定义连接器中,您可以检查在上述步骤中设置的属性.如果设置了,您可以像为 ClosableHttpClient 指定的那样设置 DefaultAuthSchemeRegistry(ApacheConnector 在其实现中使用 ClosableHttpClient).

3) The Custom connector gets called when you call builder.get( or post or any other method). In the custom connector you can check for the property set in the above step. If it is set, you can set the DefaultAuthSchemeRegistry just like it is specified for ClosableHttpClient(ApacheConnector uses ClosableHttpClient in its implementation).

这可能是一种黑客行为,但对我来说效果很好.希望这会有所帮助:)

This may be kind of a hack but works fine for me. Hope this helps :)

这篇关于JCIFS 可以与 Jersey 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆