I am using below code as proxy for NTLM. However in production code is either taking machine logged in use or machine name at exchange service side and not the user specified in the code. can someone please help disabling default NTLM authentication and let code only pass what is passed through NTLM code. Due to product restriction, I can only use below Authentication class, Is there a way to disable default authentication and only send NTLM what is specified in code?
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection
public class DoNTLMDo_NTLM{
protected String stringURL = "";
protected String SoapAction = "";
protected String SOAPxml = "";
protected String UserName = "";
protected String Password = "";
protected String Domain = "";
protected String ContentType = "";
protected String charset = "";
protected String Reply_SOAPxml = "";
public String getstringURL() {
return stringURL;
}
public void setstringURL(String val) {
stringURL = val;
}
public String getSoapAction() {
return SoapAction;
}
public void setSoapAction(String val) {
SoapAction = val;
}
public String getSOAPxml() {
return SOAPxml;
}
public void setSOAPxml(String val) {
SOAPxml = val;
}
public String getUserName() {
return UserName;
}
public void setUserName(String val) {
UserName = val;
}
public String getPassword() {
return Password;
}
public void setPassword(String val) {
Password = val;
}
public String getDomain() {
return Domain;
}
public void setDomain(String val) {
Domain = val;
}
public String getContentType() {
return ContentType;
}
public void setContentType(String val) {
ContentType = val;
}
public String getcharset() {
return charset;
}
public void setcharset(String val) {
charset = val;
}
public String getReply_SOAPxml() {
return Reply_SOAPxml;
}
public void setReply_SOAPxml(String val) {
Reply_SOAPxml = val;
}
public ProxyDoNTLMDo_NTLM() {
}
public void invoke() throws Exception {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Authenticator.setDefault(new MyAuthenticator(getDomain()+"\\"+ getUserName(), getPassword()));
try {
URL url = new URL( getstringURL());
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(50000);
connection.setConnectTimeout(50000);
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setRequestProperty("Content-Type", getContentType());
connection.setRequestProperty("SOAPAction", getSoapAction());
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(getSOAPxml());
writer.flush();
writer.close();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, getcharset());
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine();
if (s == null)
break;
setReply_SOAPxml(s); //System.out.println(s);
}
// System.out.println("Done");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
class MyAuthenticator extends Authenticator {
private String httpUsername;
private String httpPassword;
public MyAuthenticator(String httpUsername, String httpPassword) {
this.httpUsername = httpUsername;
this.httpPassword = httpPassword;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(httpUsername, httpPassword.toCharArray());
}
}
}