我正在尝试使用 NTLM 代理进行 Jersey 客户端调用?这可能吗,因为我无法获得任何明确的信息。有人试过吗?
最佳答案
是的,可以配置 Jersey Client通过需要 NTLM 身份验证的代理服务器进行连接。
这是一个简化的代码片段,它准备了一个适合 Jersey v2.5+ 的 ClientConfig
:
final ClientConfig config = new ClientConfig();
config.property(ClientProperties.PROXY_URI, "http://myproxy.com:8000");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope ntlmAuthScope =
new AuthScope("myproxy.com", 8000, AuthScope.ANY_REALM, "NTLM");
credentialsProvider.setCredentials(
ntlmAuthScope,
new NTCredentials("user", "password", "hostname", "domain") );
config.property(
ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
config.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(config);
请注意:我将 Apache HttpClient 连接器与 Jersey 客户端一起使用 - 如果您使用 another client transport connector,您可能需要稍微不同的代码.
如果您希望您的 POST/PUT 请求被缓冲(因此可重复)以响应从您的代理服务器返回的任何 407 身份验证挑战,您可能还需要将以下行添加到您的代码中:
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING,
RequestEntityProcessing.BUFFERED);
关于java - Jersey 客户端是否支持 NTLM 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330767/