Ana içeriğe geç

NTLM Authentication ile Token Alma

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 - TÜM sertifikaları kabul et
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial(null, new TrustStrategy() {
boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) {
return true // Tüm sertifikaları güvenilir say
}
})
.build()

// SSL Socket Factory - Hostname verification'ı devre dışı bırak
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext,
NoopHostnameVerifier.INSTANCE
)

// HttpClient oluştur
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()
}

Açıklama

Bu script şu işlemleri gerçekleştirir:

  1. NTLM Kimlik Bilgileri Tanımı
  • Username, password, domain ve workstation bilgileri ile NTCredentials oluşturulur
  1. Credentials Provider
  • Apache HttpClient için NTLM destekli credentials provider tanımlanır
  1. SSL Trust Override
  2. Hostname verification devre dışı bırakılır
  3. NTLM destekli HttpClient ile protected endpoint’e GET isteği atılır