我使用下面的代码作为 NTLM 的代理。然而,在生产代码中,要么是机器登录使用,要么是交换服务端的机器名称,而不是代码中指定的用户。有人可以帮助禁用默认的 NTLM 身份验证,让代码只通过 NTLM 代码传递的内容。由于产品限制,我只能使用以下身份验证类,有没有办法禁用默认身份验证,只发送代码中指定的 NTLM?
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());
}
}
}