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

查看:198
本文介绍了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/jersey/apache/connector ).就我而言,我必须创建自定义的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客户端中为客户端设置属性的方式).

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或任何其他方法)时,将调用Custom连接器.在自定义连接器中,您可以检查在上述步骤中设置的属性.如果已设置,则可以设置DefaultAuthSchemeRegistry,就像为ClosableHttpClient指定的一样(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天全站免登陆