使用NTLM进行身份验证时,HttpClient 4.1.1返回401,浏览器工作正常

使用NTLM进行身份验证时,HttpClient 4.1.1返回401,浏览器工作正常,第1张

使用NTLM进行身份验证时,HttpClient 4.1.1返回401,浏览器工作正常

我不是该主题的专家,但是在使用http组件进行NTLM身份验证期间,我发现客户端需要进行3次尝试才能连接到NTML端点。这里对Spnego进行了描述,但对于NTLM身份验证则有所不同。

对于第一次尝试使用NTLM的客户端,客户端将发出请求

Target auth state: UNCHALLENGED
并且Web服务器将返回HTTP
401状态和标头:
WWW-Authenticate: NTLM

客户端将检查配置的身份验证方案,应在客户端代码中配置NTLM。

第二次尝试,客户端将使用发送请求

Target auth state:CHALLENGED
,并发送带有以base64格式编码的令牌的授权标头:
Authorization: NTLMTlRMTVNTUAABAAAAAYIIogAAAAAoAAAAAAAAACgAAAAFASgKAAAADw==
服务器再次返回HTTP
401状态,但标头:
WWW-Authenticate: NTLM
现在已填充编码信息。

3rd Attempt客户端将使用

WWW-Authenticate: NTLM
标头中的信息,并通过
Target auth state:HANDSHAKE
Authorization: NTLM
包含服务器更多信息的授权标头进行最终请求。

就我而言,

HTTP/1.1 200 OK
之后我会收到。

为了避免所有请求文档中的所有问题,第4.7.1章指出,对于逻辑相关的请求,必须使用相同的执行令牌。对我来说,它没有用。

我的代码:我

@PostConstruct
使用EJB方法初始化一次客户端

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();        cm.setMaxTotal(18);        cm.setDefaultMaxPerRoute(6);        RequestConfig requestConfig = RequestConfig.custom()        .setSocketTimeout(30000)        .setConnectTimeout(30000)        .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))        .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))        .build();        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();        credentialsProvider.setCredentials(AuthScope.ANY,     new NTCredentials(userName, password, hostName, domainName));        // Finally we instantiate the client. Client is a thread safe object and can be used by several threads at the same time.         // Client can be used for several request. The life span of the client must be equal to the life span of this EJB.         this.httpclient = HttpClients.custom()        .setConnectionManager(cm)        .setDefaultCredentialsProvider(credentialsProvider)        .setDefaultRequestConfig(requestConfig)        .build();

在每个请求中使用相同的客户端实例:

 HttpPost httppost = new HttpPost(endPoint.trim());  // HttpClientContext is not thread safe, one per request must be created. HttpClientContext context = HttpClientContext.create();     response = this.httpclient.execute(httppost, context);

在我的EJB的@PreDestroy方法中,释放资源并将连接返回到连接管理器:

  this.httpclient.close();


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5428510.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存