Ana içeriğe geç

Retrieving Token with NTLM Authentication

Groovy Script

import org.apache.http.auth.AuthScope
import org.apache.http.auth.NTCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.methods.HttpGet
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
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.HttpClients
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.util.EntityUtils

import javax.net.ssl.SSLContext

// NTLM Credentials
def username = "kullanici"
def password = "sifre"
def domain = "DOMAIN"
def workstation = ""

// Credentials Provider
CredentialsProvider credsProvider = new BasicCredentialsProvider()
credsProvider.setCredentials(
AuthScope.ANY,
new NTCredentials(username, password, workstation, domain)
)

// SSL Context - Trust ALL certificates
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial(null, new TrustStrategy() {
boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) {
return true
}
})
.build()

// SSL Socket Factory - Disable hostname verification
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext,
NoopHostnameVerifier.INSTANCE
)

// Create HttpClient
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setSSLSocketFactory(sslSocketFactory)
.build()

try {
HttpGet httpGet = new HttpGet("https://api.example.com/endpoint")
httpGet.addHeader("Accept", "application/json")

def response = httpClient.execute(httpGet)

try {
println "Status Code: ${response.getStatusLine().getStatusCode()}"

def entity = response.getEntity()
if (entity != null) {
String responseBody = EntityUtils.toString(entity)
println "Response: ${responseBody}"
}
} finally {
response.close()
}

} finally {
httpClient.close()
}

Description

This script performs the following operations:

  1. NTLM Credentials Definition
  • NTCredentials are created with username, password, domain, and workstation information.
  1. Credentials Provider
  • An NTLM-enabled credentials provider is defined for Apache HttpClient.
  1. SSL Trust Override
  2. Hostname verification is disabled
  3. A GET request is sent to the protected endpoint using the NTLM-enabled HttpClient.