首页 文章

GlassFish(或任何servlet容器)上的HTTP连接的JCIFS NTLM身份验证

提问于
浏览
0

我创建了一个Java类,它连接到需要NTLM身份验证的IIS网站 . 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 HTTP response code: 401 for URL: ...."的 IOException .

我已经尝试将jcifs jar移动到系统类路径(%GLASSFISH%/ lib),但这似乎没有任何区别 .

建议非常感谢 .

2 回答

  • 0

    似乎我在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();
    }
    
  • 3

    听起来像JCIFS无权设置工厂来处理Glassfish中的URL . 您应该检查策略设置(checkSetFactory) .

    Config #registerSmbURLHandler()可能会吞下SecurityException .

相关问题