0

NTLM 認証を必要とする IIS Web サイトに接続する 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);
}

この例は、コマンド プロンプトから実行すると正常に動作しますが、サーブレット コンテナー (具体的には GlassFish) で同じコードを使用しようとするとすぐに、IOException「サーバーが HTTP 応答コードを返しました: 401 for URL: .. ..」。

jcifs jar をシステム クラスパス (%GLASSFISH%/lib) に移動しようとしましたが、違いはないようです。

提案は大歓迎です。

4

2 に答える 2

3

私がやろうとしていたことはすでにJava5/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-27T22:16:52.930 に答える
0

JCIFSには、Glassfish内でURLを処理するためのファクトリを設定する権利がないようです。ポリシー設定を確認する必要があります(checkSetFactory)。

Config#registerSmbURLHandler()は、SecurityExceptionを飲み込む可能性があります。

于 2009-06-26T10:40:48.233 に答える