我创建了一个Java应用程序来连接IIS 7(Windows Server 2008 R2)中托管的ASP.Net Asmx Web服务。托管站点被配置为Windows身份验证的身份验证类型,并且提供程序的身份协商和ntlm
在Java代码中,我使用Authenticator Class设置用户名和密码。如果我输入的密码错误,此代码将连接到Web服务事件。我检查了IIS日志,它实际上使用了当前登录的用户来连接Web服务。
我在调用getPasswordAuthentication()
函数后尝试调试Java代码。java代码使用krb5身份验证方法验证了用户名和密码。在这一点上,这引发了异常
“无法找到Kerberos领域”
但是此异常是在Java api上处理的,并且我正在从Web服务获取响应
如果提供的凭证错误,我们的要求是使用提供的凭证连接Web服务。它应该返回未经授权的访问代码。
下面是我的Java代码。
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.Base64;
public class ConnectToUrlUsingBasicAuthentication {
public static void main(String[] args) {
try {
String webPage = "http://servername/webservice/ABC.asmx/GetStudentReport";
String name = "domain.lab\\sachin";
String password = "Password123";
//NtlmHandler handler = new NtlmHandler();
Authenticator.setDefault(new NtlmAuthenticator(name, password));
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setRequestProperty("Authorization", "Basic " +authStringEnc );
conn.connect();
System.out.println("Response Code: " + conn.getResponseCode() );
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.println("*** BEGIN ***");
System.out.println(result);
System.out.println("*** END ***");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面是NtlmAuthenticator类
import java.net.Authenticator;
import java.net.PasswordAuthentication;
public class NtlmAuthenticator extends Authenticator {
private final String username;
private final char[] password;
public NtlmAuthenticator(final String username, final String password) {
super();
this.username = new String(username);
this.password = password.toCharArray();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("Scheme:" + getRequestingScheme() );
System.out.println("Host:" + getRequestingHost() );
PasswordAuthentication pa = new PasswordAuthentication(username, password);
System.out.println("UserName:" + pa.getUserName() );
System.out.println("Password:" + pa.getPassword().toString() );
return pa;
}
}
请提前提供建议谢谢。
参考方案
如果您在Windows上运行客户端,则此行为是指定的。身份验证过程将首先尝试使用登录的用户凭据。
这是在http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-auth.html的NTLM部分下指定的:“在Microsoft Windows平台上,NTLM身份验证尝试从系统获取用户凭据而不提示用户的身份验证器对象”。
Java-搜索字符串数组中的字符串 - java在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
Java:找到特定字符并获取子字符串 - java我有一个字符串4.9.14_05_29_16_21,我只需要获取4.9。数字各不相同,所以我不能简单地获得此char数组的前三个元素。我必须找到最正确的.并将其子字符串化直到那里。我来自Python,因此我将展示Python的实现方法。def foobar(some_string): location = some_string.rfind('.&…
Java RegEx中的单词边界\ b - java我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…
如何转义将成为字符串一部分的斜线? - java我很难找到一种方法来逃避这些引用,String key = StringUtils.substringBetween(html, "class=\"category_keyword\"", ">"); 特别是这部分:"class=\"category_keyword…
Java Double与BigDecimal - java我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…