我已经创建了连接到IIS网站需要NTLM身份验证的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),我得到包含该消息的IOException“服务器返回的HTTP响应代码:401为地址:......“

我试图移动JCIFS罐子系统类路径(%GLASSFISH%/ IIb)的,但似乎没有任何区别。

建议的高度赞赏。

有帮助吗?

解决方案

似乎有什么我试图做在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();
}

其他提示

听起来JCIFS没有设置工厂内处理的Glassfish网址的权利。您应该检查的策略设置(器checkSetFactory)。

在配置#registerSmbURLHandler()可能吞下SecurityException异常。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top