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.
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;
public class Ntlm {
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();
}
}
}
Magauiya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.