簡單的ntlm測試程式

  • 372
  • 0

ntlm是微軟的協定認證,全名是NT LAN Manager,在做SSRS (Reporting Service)的整合式有接觸到這個認證,雖然最終和預期想要的做法有差異而放棄,不過還是記錄一下我用Java去向SSRS進行ntlm authenitation請求的測試程式

 

List<String> authpref = new ArrayList<String>();
authpref.add(AuthPolicy.NTLM);
httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);

//須填入ntmli請求的username, password, workstation, domain
NTCredentials creds = new NTCredentials("username", "password", "workstation", "domain");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);


String ip = "192.168.1.100"; //ntlm ip
int port = 8080; //ntlm port
String protocol = "http"; //http or https

HttpHost target = new HttpHost(ip, port, protocol);

// Make sure the same context is used to execute logically related requests
HttpContext localContext = new BasicHttpContext();

// 先執行一個簡單的請求. This will trigger NTLM authentication
HttpGet httpget = new HttpGet("/index.html");

HttpEntity entity = response.getEntity();
System.out.prinltn(EntityUtils.toString(entity))