GlassFishの上のHTTP接続のJCIFS NTLM認証(または任意のサーブレットコンテナ)
質問
私は、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レスポンスコードを返します: URLのための401:.... "
私は、システムクラスパス(%のGlassFish%/ libに)にJCIFSジャーを移動しようとしたが、それはすべての違いを確認していないようです。
提案が高く評価されています。
解決
私は何をしようとしていたことは、すでに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の内側にあなたのURLを処理するための工場を設定する権利を持っていないように
サウンド。あなたは、ポリシー設定(のcheckSetFactory)を確認する必要があります。
コンフィグ#registerSmbURLHandler()、SecurityExceptionを飲み込む可能性があります。
所属していません StackOverflow