Http request to a domain site with NTLM authentication (Active directory). Java (apache htppclient 4.5.14)

I need to write an algorithm that sends a request to a remote server for Ntlm authorization (login and password) to a secure domain site and authorization through active directory. And I need to get a response from the server. My code should ignore certificates but it gives an error: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. and I can’t install the certificate.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import org.apache.http.auth.AuthScope;import org.apache.http.auth.NTCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.SSLContextBuilder;import org.apache.http.conn.ssl.TrustStrategy;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.ssl.SSLContexts;import org.apache.http.util.EntityUtils;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLContext;import java.io.IOException;import java.net.URI;
</code>
<code>import org.apache.http.auth.AuthScope;import org.apache.http.auth.NTCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.SSLContextBuilder;import org.apache.http.conn.ssl.TrustStrategy;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.ssl.SSLContexts;import org.apache.http.util.EntityUtils;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLContext;import java.io.IOException;import java.net.URI; </code>
import org.apache.http.auth.AuthScope;import org.apache.http.auth.NTCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.SSLContextBuilder;import org.apache.http.conn.ssl.TrustStrategy;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.ssl.SSLContexts;import org.apache.http.util.EntityUtils;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLContext;import java.io.IOException;import java.net.URI;
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Ntlm {
</code>
<code>public class Ntlm { </code>
public class Ntlm {
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void main(String[] args) throws Exception {
String domain = ""; // Replace with your domain
String username = ""; // Replace with your username
String password = ""; // Replace with your password
String targetUrl = ""; // Replace with your target URL
// Create NTLM credentials
NTCredentials credentials = new NTCredentials(username, password, null, domain);
// Create credentials provider
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials);
// Create a custom SSL context that trusts all certificates
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial((TrustStrategy) (chain, authType) -> true)
.build();
// Create a hostname verifier that accepts any hostname
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// Create an SSL socket factory with the custom SSL context
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(`your text`
sslContext,
hostnameVerifier);
// Configure the connection manager to use the SSL socket factory
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100); // Set the maximum number of connections
// Create an HTTP client with the custom SSL socket factory and credentials provider
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(cm)
.setDefaultCredentialsProvider(credProvider)
.setSSLSocketFactory(sslsf) // Set the custom SSL socket factory
.build();
// Create an HTTP GET request to the target URL
HttpGet httpGet = new HttpGet(URI.create(targetUrl));
// Create a context for the HTTP client with NTLM authentication
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credProvider);
// Execute the request and get the response
try (CloseableHttpResponse response = httpClient.execute(httpGet, context)) {
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
if (statusCode == 200) {
System.out.println("Login successful");
} else {
System.out.println("Login failed with status code: " + statusCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
</code>
<code>public static void main(String[] args) throws Exception { String domain = ""; // Replace with your domain String username = ""; // Replace with your username String password = ""; // Replace with your password String targetUrl = ""; // Replace with your target URL // Create NTLM credentials NTCredentials credentials = new NTCredentials(username, password, null, domain); // Create credentials provider CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials); // Create a custom SSL context that trusts all certificates SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial((TrustStrategy) (chain, authType) -> true) .build(); // Create a hostname verifier that accepts any hostname HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; // Create an SSL socket factory with the custom SSL context SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(`your text` sslContext, hostnameVerifier); // Configure the connection manager to use the SSL socket factory PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); // Set the maximum number of connections // Create an HTTP client with the custom SSL socket factory and credentials provider CloseableHttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(cm) .setDefaultCredentialsProvider(credProvider) .setSSLSocketFactory(sslsf) // Set the custom SSL socket factory .build(); // Create an HTTP GET request to the target URL HttpGet httpGet = new HttpGet(URI.create(targetUrl)); // Create a context for the HTTP client with NTLM authentication HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credProvider); // Execute the request and get the response try (CloseableHttpResponse response = httpClient.execute(httpGet, context)) { int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status Code: " + statusCode); System.out.println("Response Body: " + responseBody); if (statusCode == 200) { System.out.println("Login successful"); } else { System.out.println("Login failed with status code: " + statusCode); } } catch (IOException e) { e.printStackTrace(); } finally { httpClient.close(); } } </code>
public static void main(String[] args) throws Exception {
    String domain = ""; // Replace with your domain
    String username = ""; // Replace with your username
    String password = ""; // Replace with your password
    String targetUrl = ""; // Replace with your target URL

    // Create NTLM credentials
    NTCredentials credentials = new NTCredentials(username, password, null, domain);

    // Create credentials provider
    CredentialsProvider credProvider = new BasicCredentialsProvider();
    credProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials);

    // Create a custom SSL context that trusts all certificates
    SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial((TrustStrategy) (chain, authType) -> true)
            .build();

    // Create a hostname verifier that accepts any hostname
    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

    // Create an SSL socket factory with the custom SSL context
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(`your text`
            sslContext,
            hostnameVerifier);

    // Configure the connection manager to use the SSL socket factory
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(100); // Set the maximum number of connections

    // Create an HTTP client with the custom SSL socket factory and credentials provider
    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(cm)
            .setDefaultCredentialsProvider(credProvider)
            .setSSLSocketFactory(sslsf) // Set the custom SSL socket factory
            .build();

    // Create an HTTP GET request to the target URL
    HttpGet httpGet = new HttpGet(URI.create(targetUrl));

    // Create a context for the HTTP client with NTLM authentication
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credProvider);

    // Execute the request and get the response
    try (CloseableHttpResponse response = httpClient.execute(httpGet, context)) {
        int statusCode = response.getStatusLine().getStatusCode();
        String responseBody = EntityUtils.toString(response.getEntity());

        System.out.println("Status Code: " + statusCode);
        System.out.println("Response Body: " + responseBody);

        if (statusCode == 200) {
            System.out.println("Login successful");
        } else {
            System.out.println("Login failed with status code: " + statusCode);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpClient.close();
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>}
</code>
<code>} </code>
}

New contributor

Magauiya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật