Apache HttpClient v5.0在Windows上无法使用NTLM进行身份验证。
创始人
2024-09-04 04:32:38
0

在Apache HttpClient v5.0上,使用NTLM进行身份验证需要进行一些额外的配置。下面是一个使用NTLM进行身份验证的示例代码:

import org.apache.hc.client5.http.auth.*;
import org.apache.hc.client5.http.auth.authenticator.*;
import org.apache.hc.client5.http.auth.params.AuthPNames;
import org.apache.hc.client5.http.config.*;
import org.apache.hc.client5.http.impl.*;
import org.apache.hc.client5.http.impl.auth.*;
import org.apache.hc.client5.http.impl.auth.win.*;
import org.apache.hc.client5.http.impl.classic.*;
import org.apache.hc.client5.http.protocol.*;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.auth.*;
import org.apache.hc.core5.http.protocol.*;
import org.apache.hc.core5.ssl.*;
import org.apache.hc.core5.util.*;

public class HttpClientNTLMExample {

    public static void main(String[] args) throws Exception {
        // 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();

        try {
            // 配置NTLM认证
            AuthSchemeProvider authSchemeProvider = new NTLMSchemeFactory();
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials("username", "password", "workstation", "domain"));

            // 创建认证上下文
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(new HttpHost("hostname", 8080, "http"), basicAuth);
            HttpClientContext context = HttpClientContext.create();
            context.setCredentialsProvider(credentialsProvider);
            context.setAuthSchemeRegistry(AuthSchemeRegistryBuilder.create()
                    .register(AuthSchemes.NTLM, authSchemeProvider)
                    .register(AuthSchemes.BASIC, new BasicSchemeFactory(StandardCharsets.ISO_8859_1))
                    .build());
            context.setAuthCache(authCache);

            // 创建请求
            HttpGet httpGet = new HttpGet("http://hostname:8080/api/endpoint");

            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpGet, context);
            try {
                // 处理响应
                int statusCode = response.getCode();
                HttpEntity entity = response.getEntity();
                // ...
            } finally {
                response.close();
            }
        } finally {
            httpClient.close();
        }
    }
}

请确保将示例代码中的usernamepasswordworkstationdomainhostnamehttp://hostname:8080/api/endpoint替换为实际的值。

此外,还需确保在项目中包含以下依赖项:


    org.apache.httpcomponents.client5
    httpclient5
    5.0.4


    org.apache.httpcomponents.client5
    httpclient5-win
    5.0.4


    org.apache.hc.core5
    httpcore5
    5.0.4


    org.apache.hc.core5
    httpcore5-win
    5.0.4

这些依赖项将确保在Windows上使用NTLM进行身份验证时所需的类和功能。

相关内容

热门资讯

第5新版(来玩app)外挂透明... 此外,数据分析德州(来玩app)辅助神器app还具备辅助透视行为开挂功能,通过对客户来玩app透明挂...
8个打法!wepoke透明挂辅... 8个打法!wepoke透明挂辅助软件是真的(插件)透视(存在有挂)-哔哩哔哩;玩家必备必赢加哟《13...
四小程序!雀神微信小程序怎么开... 您好,雀神微信小程序怎么开挂这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多...
1个大厅!德扑ai操作(手牌胜... 1个大厅!德扑ai操作(手牌胜率计算工具)辅助透视(有挂实锤)-哔哩哔哩1、每一步都需要思考,不同水...
九黑科技(wepoke app... 九黑科技(wepoke app)外挂透明挂辅助APP(透视)技巧教程(真是有挂)-哔哩哔哩;wepo...
5个app技巧!微扑克机制技巧... 1、5个app技巧!微扑克机制技巧(插件)辅助透视(新版有挂)-哔哩哔哩(UU poker、微扑克机...
第五真的有挂!雀神微信小程序免... 第五真的有挂!雀神微信小程序免费辅助器(辅助挂)太坑了原来真的有挂(有挂透明挂)-哔哩哔哩1、用户打...
第一个脚本!德扑之星软件模拟器... 第一个脚本!德扑之星软件模拟器(作弊)透视辅助(揭秘有挂)-哔哩哔哩;支持多人共享记分板与复盘,通过...
四个规律!wepoke科技(软... 四个规律!wepoke科技(软件透明挂)辅助挂(有挂猫腻)-哔哩哔哩;wepoke科技黑科技是一款具...
第三开挂(aapoker开发)... 第三开挂(aapoker开发)外挂透明挂辅助机制(透视)高科技教程(有挂讲解)-哔哩哔哩是一款可以让...