我已经创建了一个连接到需要NTLM身份验证的IIS网站的Java类。Java类使用JCIFS库并基于以下示例:
Config.registerSmbURLHandler();
Config.setProperty("jcifs.smb.client.domain", domain);
Config.setProperty("jcifs.smb.client.username", user);
Config.setProperty("jcifs.smb.client.password", password);
URL url = new URL(location);
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}这个示例在命令提示符下执行得很好,但是只要我尝试在servlet容器中使用相同的代码(特别是GlassFish),就会得到一个包含消息"Server returned response code: 401for URL:...“的IOException。
我尝试将jcifs jar移到系统类路径(%GLASSFISH%/lib),但似乎没有任何区别。
我们非常感谢您的建议。
发布于 2009-06-27 22:16:53
似乎我要做的事情在Java 5/6中已经得到了支持,因此我能够放弃JCIFS API,转而做如下所示:
public static String getResponse(final ConnectionSettings settings,
String request) throws IOException {
String url = settings.getUrl() + "/" + request;
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
System.out.println(getRequestingScheme() + " authentication")
// Remember to include the NT domain in the username
return new PasswordAuthentication(settings.getDomain() + "\\" +
settings.getUsername(), settings.getPassword().toCharArray());
}
});
URL urlRequest = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
StringBuilder response = new StringBuilder();
InputStream stream = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str = "";
while ((str = in.readLine()) != null) {
response.append(str);
}
in.close();
return response.toString();
}发布于 2009-06-26 10:40:48
听起来JCIFS没有权利设置工厂来处理Glassfish中的URL。您应该检查策略设置(checkSetFactory)。
Config#registerSmbURLHandler()可能会吞下SecurityException。
https://stackoverflow.com/questions/1048223
复制相似问题